viewWillDisappear 内的 UIAlertView 未调用 clickedButtonAtIndex
我在一个类中有以下代码,尽管当视图即将消失时,警报确实出现在 UI 中(使用 iOS SDK 5.0),但永远不会调用 clickedButtonAtIndex 方法,并且应用程序会以“ EXC_BAD_ACCESS”。我验证了视图确实/正在使用我的类作为委托。
代码位于主线程上,在查看了有关此主题的所有其他响应后,我不明白为什么我的委托方法从未被调用。我可以使用另一个线索。
@interface ConnectionViewController : UIViewController <UIAlertViewDelegate> {
....
}
@implementation ConnectionViewController
...
- (void)viewWillDisappear:(BOOL)animated
{
connection = [Connection objectWithConnName:[connectionName text] host:[mtDevice text] user:[userName text] passwd:[userPassword text]];
BOOL result = [connection test];
if (result) {
[[FirstViewController sharedInstance] addConnection:connection];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Failed to connect to device" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ignore", @"Ok", nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"clickedButtonAtIndex: %d",buttonIndex);
}
I have the following code in a class, although the alert does appear in the UI (using iOS SDK 5.0) when the view is about to disappear, the clickedButtonAtIndex method is never called and the app terminates with "EXC_BAD_ACCESS". I validated the view does/is using my class as a delegate.
The code is on the main thread and after looking at all the other responses on this subject I fail to see why my delegate method is never called. I could use another clue folks.
@interface ConnectionViewController : UIViewController <UIAlertViewDelegate> {
....
}
@implementation ConnectionViewController
...
- (void)viewWillDisappear:(BOOL)animated
{
connection = [Connection objectWithConnName:[connectionName text] host:[mtDevice text] user:[userName text] passwd:[userPassword text]];
BOOL result = [connection test];
if (result) {
[[FirstViewController sharedInstance] addConnection:connection];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Failed to connect to device" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ignore", @"Ok", nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"clickedButtonAtIndex: %d",buttonIndex);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
警报视图不会阻塞 - 本质上,当您选择一个选项时,您的视图控制器已经看到 viewWillDisappear、viewDidDisappear 以及可能的 dealloc,这意味着它不再存在。假设您使用的是 UINavigationController,如果想法是在返回之前提示用户,您应该
在 UINavigationBar 中重写,其中 ExitConfirmDelegate 是带有 BOOL shouldConfirmExit 的协议。如果待处理的警报视图可见,您的视图控制器将实现此协议并返回“NO”。然后,当用户单击某个选项时,只需从 clickedButtonAtIndex 方法再次调用 popViewControllerAnimated 即可。
The alert view does not block - essentially by the time you choose an option, your view controller has seen viewWillDisappear, viewDidDisappear, and probably dealloc, meaning it doesn't exist anymore. Assuming you're using a UINavigationController, if the idea is to prompt the user before navigating back, you should override
in your UINavigationBar where ExitConfirmDelegate is a protocol with BOOL shouldConfirmExit. Your view controller would implement this protocol and return 'NO' if a pending alert view is visible. Then, when the user does click an option, simply call popViewControllerAnimated again from your clickedButtonAtIndex method.
确保在调用“clickedButtonAtIndex”方法之前未释放 ConnectionViewController。
Make sure ConnectionViewController is not released before the "clickedButtonAtIndex" method is called.