提问人:Mor Goren 提问时间:3/11/2021 最后编辑:koenMor Goren 更新时间:3/15/2021 访问量:44
调用 init 的 func 后,UIAlertController 保持 nil
after calling an func for init the UIAlertController remains nil
问:
我有 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
答:
1赞
koen
3/12/2021
#1
[self initAlert: notesAlert];
不会创建 .相反,您可以使用notesAlert
notesAlert = [self initAlert];
也许是这样的:
@interface ViewController () {
UIAlertController *tableAlert;
UIAlertController *notesAlert;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableAlert = [self initAlert];
self.notesAlert = [self initAlert];
}
// func to init the alerts
- (UIAlertController *) initAlert {
UIAlertController *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];
return alert;
}
评论