iOS - 从另一个选项卡转到某个选项卡的第二个 uiviewcontroller [英] iOS - go to second uiviewcontroller of some tab from another tab

查看:33
本文介绍了iOS - 从另一个选项卡转到某个选项卡的第二个 uiviewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Storyboard 开发 iOS 5 应用程序.

I'm developing an iOS 5 app using Storyboard.

我有 UITabBarController 有 3 个标签.每个选项卡都有自己的 UINavigationController.UINavigationController 的第一个选项卡有一个 UITableViewController 与另一个 UIViewController 等.第二个选项卡的 UINavigationController 只有一个 <代码>UIViewController.

I have UITabBarController which has 3 tabs. Every tab has its own UINavigationController. UINavigationController of first tab has one UITableViewController which segues to another UIViewController etc. UINavigationController of second tab has only one UIViewController.

我想要实现的是在 UIViewController 中发生某些操作时导航到 UIViewController(第一个选项卡的 UINavigationController 中的第二个视图)第二个标签.

What I want to achieve is to navigate to UIViewController (second view in UINavigationController of first tab) when certain action happens in UIViewController of second tab.

我尝试使用 push segue 来完成此操作,但随后导航堆栈与不同选项卡的视图控制器混合在一起(目标视图控制器中的后退按钮指向另一个选项卡的视图控制器).

I've tried to accomplish this using push segue but then the navigation stack becomes mixed with view controllers of different tabs (the back button in destination view controller points to view controller of another tab).

所以我想知道在不同选项卡的视图控制器之间完成这种转换的正确方法"是什么.

So I want to know what is the "correct way" to accomplish this kind of transition between view controllers of different tabs.

任何帮助将不胜感激.

推荐答案

您不能使用通知,因为第一个选项卡的第一个 viewController 甚至可能不会被加载.

You can't use notifications as the first viewController of the first tab might not even be loaded.

所以一种方法是保存首选项(可能是 nsuserdefaults),然后将选项卡切换到第一个选项卡,并在第一个选项卡的第一个 viewController 中在 viewDidAppear 或 viewWillAppear 中检查此首选项,如果已设置,则重置首选项(下次)并自动导航(推送)到第二个 viewController.

So one way you could do it is save a preference (nsuserdefaults perhaps), then switch the tab to the first one, and in the first viewController of the first tab check this preference in viewDidAppear or viewWillAppear, and if it's set then reset the preference (for next time) and auto navigate (push) to the second viewController.

BOOL shouldAutoPush=YES;
[[NSUserDefaults standardUserDefaults] shouldAutoPush forKey:@"shouldAutoPush"];
[[NSUserDefaults standardUserDefaults] synchronize]; 
[self.tabBarController setSelectedIndex:0];

-(void)viewWillAppear:(BOOL)animated {
     if ( [[NSUserDefaults standardUserDefaults] objectForKey:@"shouldAutoPush"] ) {
        BOOL shouldAutoPush=NO;
        [[NSUserDefaults standardUserDefaults] shouldAutoPush forKey:@"shouldAutoPush"];
        [[NSUserDefaults standardUserDefaults] synchronize]; 
        [self.navigationController pushViewController:secondViewController animated:YES];                
     }
}

这篇关于iOS - 从另一个选项卡转到某个选项卡的第二个 uiviewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆