使用 UIAlertView 防止更改 Tabbar 视图控制器

发布于 2024-11-16 21:19:31 字数 996 浏览 3 评论 0原文

我试图让当用户按下选项卡栏项目时调用 UIAlertView,询问是否真的想更改实际选项卡,问题是 UIAlertView 在获得答案之前不会停止代码,代码会继续运行并根据先前的值更改视图控制器或不更改,而不是实际值。

我试图等待一段时间才能得到答案,但屏幕只会变暗,而且警报也没有弹出。我还阅读了这篇文章暂停代码执行直到 UIAlertview,我尝试过,但无法使其工作,有人可以帮忙吗,谢谢!

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

if (([self Myfunction]) && (viewController != [tabBarController.viewControllers objectAtIndex:0])){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    return boolean_var;
}

return YES;}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) [self setBoolean_var:NO];
else [self setBoolean_var:YES];}

I'm trying to make that when the user press a tabbar item an UIAlertView gets called, asking if really wants to change the actual tab, the problem is that the UIAlertView doesn't stop the code until getting the answer, the code keeps running and depending on the previous value change the viewcontroller or not, not the actual.

I've tried to wait to the answer with a while, but the screen only gets darker and the alert didn't popup. I also read this post pause code execution until UIAlertview, I tried but i was unable to make it work, can someone help, thanks!

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

if (([self Myfunction]) && (viewController != [tabBarController.viewControllers objectAtIndex:0])){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    return boolean_var;
}

return YES;}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) [self setBoolean_var:NO];
else [self setBoolean_var:YES];}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如荒岛 2024-11-23 21:19:31
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ( !([self Myfunction]) || (viewController == [tabBarController.viewControllers objectAtIndex:0])) {
        return YES;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    candidateViewController = viewController; // `candidateViewController` must be declared as an instance variable.

    return NO;
}

确定需要显示警报的视图控制器,并将其保存在 candidateViewController 中,并返回 NO 以延迟切换。根据警报视图上的响应,您应该更改它。

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 0)
        self.tabBarController.selectedViewController = candidateViewController;
}

最后一种方法假设了一些事情。您的标签栏控制器由 self.tabBarController 引用,并且您设置 boolean_var 将其返回到之前的方法。警报视图在该方法中是非阻塞的,因此使用 boolean_var 是没有意义的。

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ( !([self Myfunction]) || (viewController == [tabBarController.viewControllers objectAtIndex:0])) {
        return YES;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    candidateViewController = viewController; // `candidateViewController` must be declared as an instance variable.

    return NO;
}

Identify which view controller that you need to show alert for and save it in candidateViewController and return NO to delay the switch. Based on the response on the alert view, you should change it.

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 0)
        self.tabBarController.selectedViewController = candidateViewController;
}

The last method assumes few things. Your tab bar controller is referenced by self.tabBarController and that you were setting boolean_var to return it to the earlier method. Alert view is non blocking in that method so using boolean_var is pointless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文