alitosuner
alitosuner

Reputation: 1034

UIAlertController giving an error

I am new on IOS. I have a sample project and trying to learn Obj-C on that.

Now I am on the stage that is about learning how to use UIAlertController. And I have a code like that:

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

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

But When I run the simulator. There should be pop up message on Simulator. Instead of that, there is an error that says:

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

Any help would be greatly appreciated

Upvotes: 2

Views: 586

Answers (1)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

try this

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

Upvotes: 2

Related Questions