Search code examples
iphoneobjective-ciosbuttonuiactionsheet

Clicking a button in UIActionSheet causes my app to crash


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?


Solution

  • 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.