gpt4 book ai didi

ios - UIAlertController 报错

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:14 32 4
gpt4 key购买 nike

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

关于ios - UIAlertController 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064696/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com