【问题标题】:after calling an func for init the UIAlertController remains nil在为 init 调用 func 之后,UIAlertController 保持为零
【发布时间】: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


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-03
      • 2019-11-21
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多