iPhone:每次通话时 UIAlert 对话框都会出现 3 次
我有一个 UIAlert,每次调用时都会弹出 3 次。在我点击它之前它出现然后消失。难道是 viewDidLoad
本身被调用了 3 次吗?
我在应用程序的 viewDidLoad
中实现了一个 UIAlert:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:alertMessage delegate:self cancelButtonTitle:ok otherButtonTitles:nil];
这是 rootViewController 中的 viewDidLoad
,它管理视图:
- (void)viewDidLoad {
Kundenkarte *kartenAnsicht = [[Kundenkarte alloc]
initWithNibName:@"Kundenkarte" bundle:nil];
kartenAnsicht.rootViewController = self;
kartenAnsicht.viewDidLoad;
self.map = kartenAnsicht;
[self.view addSubview:kartenAnsicht.view];
[kartenAnsicht release];
// [super viewDidLoad];
}
viewDidLoad
唤起UIAlert 位于 kartenAnsicht
视图控制器中。
我希望有人能帮助我,因为我没有想法。
I have a UIAlert that pops up 3 times every time it is called. It appears and then disappears before I can click on it. Could it be that the viewDidLoad
itself is being called 3 times?
I implemented an UIAlert in the viewDidLoad
of my app:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:alertMessage delegate:self cancelButtonTitle:ok otherButtonTitles:nil];
This is the viewDidLoad
in the rootViewController, that manages the views:
- (void)viewDidLoad {
Kundenkarte *kartenAnsicht = [[Kundenkarte alloc]
initWithNibName:@"Kundenkarte" bundle:nil];
kartenAnsicht.rootViewController = self;
kartenAnsicht.viewDidLoad;
self.map = kartenAnsicht;
[self.view addSubview:kartenAnsicht.view];
[kartenAnsicht release];
// [super viewDidLoad];
}
The viewDidLoad
that evokes the UIAlert is in the kartenAnsicht
view controller.
I hope someone can help me because I'm out of ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要自己调用
-viewDidLoad
,它是由NIB加载机制自动运行的。这意味着您会获得额外的-viewDidLoad
调用:这是一种设计初衷,无论您何时调用它,都会获得额外的调用。You don't need to call
-viewDidLoad
yourself, it's run automatically by the NIB-loading mechanism. That means you get extra invocations of-viewDidLoad
: one by design, and extras whenever you call it.首先,您绝对不应该在
viewDidLoad
中放置任何类型的显示。该方法用于首次从 nib 读取视图后的幕后配置。不确定每次视图显示时都会调用它,因为在第一次加载之后,视图可能保存在内存中并且不会从 nib 重新加载。相反,请将调用调用
viewWillDisplay
或viewDidDisplay
中的 NSAlert 来调用。每次视图出现时都会显示警报。我怀疑 viewDidLoad 被调用了 3 次,但要检查这一点,只需在方法中放置一个 NSLog 来查看它被调用了多少次。
当你这么说时:
...这是什么意思?到底什么对象有该方法?如果是应用程序委托,则这将不起作用,因为应用程序委托协议不会响应viewDidLoad。它必须位于 UIViewController 中。
Edit01:
请参阅这篇文章,有同样的问题: UIAlertView 每次调用都会弹出三次,而不是一次
简短回答:您可以通过释放警报来终止警报。要么将其保留为视图控制器的属性,要么更好的是,使用
runModal
而不是show
显示警报,并捕获立即返回的按钮编号。First of all, you should never put any type of display in
viewDidLoad
. That method is intended for behind the scenes configuration after the view is first read from nib. There is no certainty that it will be called every time the view displays because after the first time it loads, the view maybe held in memory and not reloaded from nib.Instead, put the call to evoke the NSAlert in
viewWillDisplay
orviewDidDisplay
. This will display the alert each time the view appears.I doubt that
viewDidLoad
is called three times but to check for that just put an NSLog in the method to see how many times it is called.When you say that:
... what does that mean? What object exactly has the method? If it is the application delegate, this will not work because the application delegate protocol does not respond to
viewDidLoad
. It has to be in a UIViewController.Edit01:
See this post that had the same problem: UIAlertView Pops Up Three Times per Call Instead of Just Once
Short answer: You kill the alert by releasing it. Either retain it as a property of the view controller or better yet, display the alert with
runModal
instead ofshow
and capture the button number returned immediately.查看警报调用周围的代码会很有帮助。
每当可达性发生变化时,我都会使用警报。由于重复检查可达性,因此可能会重复调用警报。为了缓解这种情况,我将警报代码包装如下:
但是,这样做的一个问题是,当您单击“取消”按钮时,警报将保持非零,因此由于该条件而永远不会再次显示。如果有人可以添加此响应并修复该问题,那就太好了。我假设我可以为取消按钮添加一个处理程序,该处理程序将销毁 myAlert。
It would be helpful to see the code around the alert call.
I am using an alert whenever the reachability changes. Since reachability is checked repeatedly, the alert could get called repeatedly. To alleviate that, I wrap the alert code like so:
However, one problem with this is that when you click the Cancel button, the alert will remain non-nil, and therefore can never show again because of that conditional. If someone could add to this response with a fix for that, that would be great. I assume I can add a handler for the cancel button that will destroy myAlert.