问题
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