问题
I'm trying to implement a UIActionSheet which will have some sharing buttons on it for sharing to twitter, facebook, email and print.
Here is my share view controller.h
@interface ShareViewController : UITableViewController <UIActionSheetDelegate, MFMailComposeViewControllerDelegate>
@property (nonatomic, strong) NSMutableDictionary *services;
@property (strong, nonatomic) UIActionSheet *actionSheet;
@property (strong, nonatomic) id <ShareViewControllerDelegate> delegate;
@property (nonatomic, strong) UIPopoverController *popCon;
@property (nonatomic, strong) NSString *serviceTitle;
-(IBAction)loadActionSheet:(id)sender;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
- (void)tweetThis;
- (void)printThis;
- (void)openMail;
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error;
@end
Here is my share view controller.m
-(IBAction)loadActionSheet:(id)sender{
if (self.actionSheet) {
// do nothing
}
else {
UIActionSheet *sharing = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"Twitter", @"Facebook", @"Email", @"Print", nil];
[sharing showFromBarButtonItem:sender animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"Button index: %d",buttonIndex);
switch (buttonIndex) {
case 0:
[self tweetThis];
break;
case 1:
[self sendToFacebook];
break;
case 2:
NSLog(@"email");
[self openMail];
break;
case 3:
NSLog(@"Print");
[self printThis];
break;
default:
NSLog(@"Error");
break;
}
}
I ma initiaalising my share view controller in main view controller.m as follows:
- (IBAction)shareButtonTapped:(id)sender {
ShareViewController *svc = [[ShareViewController alloc] initWithStyle:UIActionSheetStyleDefault];
[svc loadActionSheet:(id)sender];
}
I can open the action sheet fine, but when I click on any of the buttons, it crashes the app. Any ideas as to why?
回答1:
This is most likely a memory-management issue. It looks like you're using ARC, so after the shareButtonTapped
method returns, ARC will automatically release svc
, as it's no longer referenced anywhere. The delegate property of the action sheet is weak
, so it's not retained by that either.
I don't really see why ShareViewController
is a view controller in the first place, as you never seem to be loading its view, but you may have your reasons... In any case, you should make that controller a strong
property or instance variable of your other (main) view controller, so that it's not automatically released before the action sheet finishes.
回答2:
Yes. Go to your .xib file.
You should see all the buttons you have.
Click on a button and go to the "Connections Inspector"
Delete the referencing outlet.
Drag the referencing outlet to the button and set the button to the "ID" you want.
Let me know if that works.
回答3:
Just a stupid answer - the methods like - (void) sendToFacebook; are implemented, right?
来源:https://stackoverflow.com/questions/11512056/clicking-a-button-in-uiactionsheet-causes-my-app-to-crash