在为 init 调用 func 后,UIAlertController 保持为 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,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

解决方法

In [67]: z = truncnorm.rvs(a,b,loc,scale,size=100000) In [68]: plt.hist(z,bins=100) 不会创建 [self initAlert: notesAlert];。相反,您可以使用 notesAlert

也许是这样的:

notesAlert = [self initAlert];

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...