UIAlertController 报错

UIAlertController giving an error

我是 IOS 的新手。我有一个示例项目,正在尝试学习 Obj-C。

现在我在学习如何使用UIAlertController的舞台上。 我有这样的代码:

if (loanAmount == 0) { 

 UIAlertController *ErrorMessage    =[UIAlertController alertControllerWithTitle:@"Invalid amount" message:@"Enter a valid number"   preferredStyle:UIAlertControllerStyleAlert]; 
   } else 
 { // sets labels
  self.interestLabel.text = [NSString  stringWithFormat:@"%i%c",interestRate,percentage];   
self.periodLabel.text = [NSString stringWithFormat:@"%i months",months]; 
self.totalLabel.text = [NSString stringWithFormat:@"$%i",(loanAmount + tinterest)];

============================================= ===========================

但是当我运行模拟器的时候。模拟器上应该有弹出消息。取而代之的是,出现了一个错误:

015-10-11 13:56:39.985 iBank[8055:1231178] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7ca82400>)

如有任何帮助,我们将不胜感激

试试这个

if (loanAmount == 0) { 
UIAlertController * ErrorMessage = [UIAlertController
                          alertControllerWithTitle:@"Invalid amount"
                          message:@"Enter a valid number" 
                          preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction 
        actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
                  style:UIAlertActionStyleCancel
                handler:^(UIAlertAction *action)
                {
                  NSLog(@"Cancel action");
                }];

UIAlertAction *okAction = [UIAlertAction 
        actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                  style:UIAlertActionStyleDefault
                handler:^(UIAlertAction *action)
                {
                  NSLog(@"OK action");
                }];

[ErrorMessage addAction:cancelAction];
[ErrorMessage addAction:okAction];
[self presentViewController: ErrorMessage animated:YES completion:nil];
 }
 else
 {
  self.interestLabel.text = [NSString  stringWithFormat:@"%i%c",interestRate,percentage];   
self.periodLabel.text = [NSString stringWithFormat:@"%i months",months]; 
self.totalLabel.text = [NSString stringWithFormat:@"$%i",(loanAmount + tinterest)];
  }