EXC_BAD_ACCESS on UIAlertController code = 1

五迷三道 提交于 2019-12-10 17:54:18

问题


I have a view controller from where I am launching an UIAlertController on click of a Button. Below is my code:

- (IBAction)playOnlineURL:(UIButton *)sender {
[self launchPlayURLAlert];
}

- (void) launchPlayURLAlert{

NSString *defaultURLString = @“MY URL”;
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Play Online URL"
                                                                          message: @"Enter the URL"
                                                                   preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"Enter URL";
    textField.text = defaultURLString;
    textField.textColor = [UIColor blueColor];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"Play" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    NSURL *url = [[NSURL alloc] initWithString:[[alertController textFields] firstObject].text];
    VideoPlayerVC *videoController = [[VideoPlayerVC alloc] initWithNibName:@"VideoPlayerVC"
                                                                       bundle:nil
                                                                          url:url];
    [self presentViewController:videoController animated:YES completion:nil];
}]];

[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];

}

But, my app is crashing giving EXC_BAD_ACCESS.

After trying lot of things, I finally changed the

[self presentViewController:alertController animated:YES completion:nil];

to

[self presentViewController:alertController animated:NO completion:nil];

in the above code and it started working.

So, my question is why passing animated as YES giving that crash?

Also, an interesting point to note is that if I reset my whole emulator and run the app with animated as YES then it is working for first few runs. After some X runs, it starts crashing.

来源:https://stackoverflow.com/questions/46241743/exc-bad-access-on-uialertcontroller-code-1

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