gpt4 book ai didi

objective-c - UIAlertView 在 vi​​ewWillDisappear 中

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:21 25 4
gpt4 key购买 nike

当用户按下我的 UINavigationController View 上的后退按钮时,我想显示一个带有 2 个选项的 UIAlertView。一个选项 (OK) 允许用户返回(到上一个屏幕),另一个选项 (Cancel) 将留在当前 Controller 。

我在我的 viewWillDisappear 中实现了以下代码(我在这里找到的):

-(void) viewWillDisappear:(BOOL)animated{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"All Data will be lost"
message:@"You must be connected to the internet to use this app."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.tag = 1;
[alert show];
[alert release];
}
[super viewWillDisappear:animated];

}

问题是,在我按下“返回”后, View 立即跳转到上一个屏幕,并在此屏幕上显示 UIAlertView。

在 UIAlertView 回调方法中,我使用了以下编码,但没有任何反应(我认为这是因为我已经在之前的 View 中看到过这一点):

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1)
NSLog(@"back");
//Go Back ...
}

BR,迈贝克

最佳答案

viewWillDisappear 被调用时,为时已晚。导航 Controller 已经关闭了 View Controller 。

相反,您需要拦截后退按钮触摸事件。您可以通过多种方式执行此操作 - 您可以有一个自定义后退按钮,它显示您的 UIAlertView。或者您可以使用 UINavigationBarDelegate,它有一个 navigationBar:shouldPopItem: 方法。

关于objective-c - UIAlertView 在 vi​​ewWillDisappear 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8383463/

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