UIActionSheet showInview method cause crashed the app in second time

你说的曾经没有我的故事 提交于 2019-12-11 14:36:09

问题


I am using UIActionSheet to choose options. The same code work fine in iOS 7 but the problem arise in iOS 8.

actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose an option" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:Nil otherButtonTitles:@"Option1", @"Option2", @"Option2", nil];

On a IBAction I am using the below code to show action sheet.

[actionSheet showInView:[self view]];

It's work fine for the first time. But in second time the app got crashed due to EXE_BAD_ACCESS


回答1:


Inspiring from @TonyMkenu's comment I do below and it's work.

On completion of dismissViewControllerAnimated I released the action sheet.

[self dismissViewControllerAnimated:YES completion:^{
        actionSheet =nil;
}];

And before calling I allocated the action sheet every time.

if(! actionSheet)
   actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose an option" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:Nil otherButtonTitles:@"Option1", @"Option2", @"Option2", nil];

[actionSheet showInView:[self view]];



回答2:


If the code isn't in viewcontroller,you may just make the view's window always be keywindow.

 [actionSheet showInView:[self view]];
 [self.view.window makeKeyAndVisible];



回答3:


You should be using UIAlertController in iOS 8 as UIActionSheet and UIAlertView have been replaced by it. It's quite easy and make use of blocks. Here is a sample you can work with.

- (IBAction)actionButtonAction:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Select an action" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    // These next two lines are necessary for iPad and wide layouts. 
    alertController.popoverPresentationController.sourceView = self.view;
    alertController.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width - 34, 20, 0, 0);




    [alertController addAction:[UIAlertAction actionWithTitle:@"Share Data" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // Do action Share data
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Share Plot" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // Do action Share Plot
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
         // User cancelled
    }]];

    [self presentViewController:alertController animated:YES completion:^{

    }];
}


来源:https://stackoverflow.com/questions/26399167/uiactionsheet-showinview-method-cause-crashed-the-app-in-second-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!