UIAlertController发出错误

时间:2015-10-11 11:58:01

标签: ios objective-c popup

我是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>)

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

试试这个

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)];
  }