【发布时间】:2021-06-09 11:46:27
【问题描述】:
我有 2 个要在不同情况下显示的警报,我在开始时为 init 编写了一个通用函数并更改了消息,但是当我尝试显示警报时,我遇到了崩溃。当我在运行时检查notesAlert 时,它仍然为零。
谁能解释我做错了什么?
@interface viewController (){
UIAlertController *tableAlert;
UIAlertController *notesAlert;
}
@end
@implementation viewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initAlert:tableAlert];
[self initAlert:notesAlert];
}
// func to init the alerts
-(void)initAlert:(UIAlertController*)alert{
alert = [UIAlertController alertControllerWithTitle: @"" message: @"" preferredStyle:UIAlertControllerStyleActionSheet];
[alert setModalPresentationStyle:UIModalPresentationPopover];
[alert.popoverPresentationController setSourceView:self.view];
UIPopoverPresentationController *popover = [alert popoverPresentationController];
CGRect popoverFrame = CGRectMake(0,0, self.view.frame.size.width/2, self.view.frame.size.width/2);
popover.sourceRect = popoverFrame;
UIAlertAction *dismiss = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:dismiss];
}
- (IBAction)showNotes:(id)sender {
// here the notesAlert is still nil
[notesAlert setTitle:@"oops"];
[notesAlert setMessage:@"you pressed the wrong one"];
[self presentViewController:notesAlert animated:YES completion:nil];
}
@end
【问题讨论】:
标签: objective-c null runtime-error uialertcontroller func