在 viewWillAppear、iPhone 上添加 AlertView
我试图在视图出现时立即显示警报视图(不使用按钮)。在 viewcontroller.m 中,我有:
- (void) viewWillAppear:(BOOL)animated
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"User Information" message: @"Hello"
delegate: self cancelButtonTitle:nil otherButtonTitles: @"Continue", nil];
[alert show];
[alert release];
}
在 viewcontroller.h 中,我有:
IBOutlet UIView *alert;
我没有对我的 .xib 文件执行任何操作。
在模拟器上运行时,我在控制台中得到以下内容:
2009-11-30 23:41:36.079 BatteryApp[867:20b] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UIViewController _loadViewFromNibNamed:bundle” :] 加载了“BatteryAppViewController”笔尖,但未设置视图出口。'
有什么想法是我哪里出了问题,与我的 xib 有关吗?
编辑//我将警报连接到我的 xib 中的新视图,但仍然没有运气
I am attempting to show an alert view as soon as the view appears (without using a button). In viewcontroller.m I have:
- (void) viewWillAppear:(BOOL)animated
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"User Information" message: @"Hello"
delegate: self cancelButtonTitle:nil otherButtonTitles: @"Continue", nil];
[alert show];
[alert release];
}
and in viewcontroller.h I have:
IBOutlet UIView *alert;
I haven't done anything to my .xib file.
When run on the simulator I get the following in console:
2009-11-30 23:41:36.079 BatteryApp[867:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BatteryAppViewController" nib but the view outlet was not set.'
Any ideas where I have gone wrong, something to do with my xib?
Edit // I connect alert to a new view in my xib, still no luck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题在于两个核心动画(一个用于查看当前视图,一个用于显示警报)重叠。您需要按照以下解决方法将这两个动画移动分开:
将警报代码放在
viewDidAppear
中,而不是viewWillAppear
调用
[alert PerformSelector:@selector(show) withObject:nil afterDelay:0.0]
。这可确保警报在完成当前动画后发生。您在发布警报时可能还需要小心。如果我是你,我会有一个单独的方法(例如 showHelloAlert),然后从 viewDidAppear 调用它:
The problem is that the two core animations (the one for viewing the current view, and the one for showing the alert ) overlap. You need to separate these two animation moves, by following this workaround:
Have the alert code be in
viewDidAppear
, rather thanviewWillAppear
Call
[alert performSelector:@selector(show) withObject:nil afterDelay:0.0]
. This ensures that the alert occurs after finishing the current animation.You may need to be careful about releasing
alert
quite yet. If I were you, I would have a separate method (e.g. showHelloAlert), and then call it from viewDidAppear:尝试将警报放入 viewDidAppear 中。
另外,iboutlet 是做什么用的?尝试把整个东西拿出来。
Try putting the alert in viewDidAppear.
Also, what is the iboutlet for? Try taking the whole thing out.
在 Interface Builder 中打开您的 xib 文件,然后从文件所有者(应该属于
BatteryAppViewController
类)控制拖动到您的视图(不是警报),然后从弹出的菜单中选择视图。该日志表明您尚未建立此插座连接。Open your xib file in Interface Builder, and control drag from the file owner (which should be of class
BatteryAppViewController
) to your view (not the alert) and select view from the menu that pops up. The log indicates you haven't made this outlet connection.您没有在 Interface Builder 中连接 UIAlerts。警报没有可自定义的笔尖,因此您无法在界面生成器中加载它们。您需要删除:
...完全。
该错误消息表明您没有为视图控制器定义视图。您需要在 Interface Builder 中进行设置。
You don't connect UIAlerts up in Interface Builder. Alerts do not have customizable nibs so you can't load them up in interface builder. You need to remove the:
...completely.
The error message says that you have no view defined for your view controller. You need to set that up in Interface Builder.