问题弹出到标签栏开关上的根导航控制器 [英] Problem popping to root nav controller on tab bar switch

查看:88
本文介绍了问题弹出到标签栏开关上的根导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试模仿/复制内置地址簿,特别是编辑联系人或从电话应用程序内部查看现有联系人信息时的行为。导航到另一个选项卡时,将重置编辑状态并弹出新建联系人或信息视图,以便在返回联系人选项卡时返回根表视图。

Trying to mimic/copy the built-in address book, specifically the behavior when editing a contact or viewing an existing contact's Info from inside the Phone app. When you navigate to another tab, the editing state is reset and the "New Contact" or "Info" view is popped so that when you come back to the Contacts tab, you are back at the root table view.

我使用setEditing:和popToViewController在viewWillDisappear中进行了大部分工作:但是当用户使用后退按钮从Info视图导航到表视图时,我会遇到奇怪的行为。即使我弹出到根表视图控制器,它似乎使用默认的UITableViewController类而不是我的子类(例如标准选择行为而不是我的覆盖来推送详细视图。)

I have most of this working inside viewWillDisappear using setEditing: and popToViewController: however I get strange behavior when the user navigates from the Info view to the table view using the back button. Even if I pop to the root table view controller, it seems to be using the default UITableViewController class and not my subclass (e.g. standard selection behaviors instead of my overrides to push the detail view.)

任何提示? IPD

以下是一些代码来说明:

Here's some code to illustrate:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    // This is to clean up from the colored bar in detail view
    self.navigationController.navigationBar.tintColor = nil;

    // These are to match the behaviour of Contacts app
    [self setEditing:NO animated:NO];

    // This is the tricky part: works when switching tabs, but not when back button was going to pop anyway!!
    [self.navigationController popToViewController:rootViewControllerForTab animated:NO];
}


推荐答案

-viewWillDisappear:方法是不是修改navigationController的视图控制器堆栈的最佳位置,因为它在切换选项卡时以及在视图被按下时触发。

The -viewWillDisappear: method is not the best place for modifying the view controller stack for your navigationController because it is triggered both when you switch tabs and when a view is pushed on top of it.

我玩过这个有点发现,最好的地方是 - [UITabBarControllerDelegate tabBarController:didSelectViewController:]方法。所以,首先你需要指定一个对象作为标签栏的委托(我使用了app委托)。将UITabBarController的delegate属性绑定到在代码或Interface Builder中实现UITabBarControllerDelegate协议的对象。

I played around with this a bit and found that the best place for this is in the -[UITabBarControllerDelegate tabBarController:didSelectViewController:] method. So, first you need to designate an object to be the delegate for your tab bar (I used the app delegate). Bind the delegate property of your UITabBarController to an object implementing the UITabBarControllerDelegate protocol in code or in Interface Builder.

然后,实现-tabBarController:didSelectViewController:方法。现在的诀窍是如何判断何时切换到地址簿选项卡。我使用UINavigationController类型的属性(选项卡的根视图控制器)跟踪相关选项卡的视图控制器。在使用Interface Builder将tab1NavController属性绑定到实际实例之后,可以使用它来与viewController参数进行比较,以查看刚刚选择了哪个选项卡。

Then, implement the -tabBarController:didSelectViewController: method. The trick now is how to tell when your "address book" tab is being switched to. I kept track of the view controller for the tab in question using a property of type UINavigationController (the root view controller for the tab). After binding the tab1NavController property to the actual instance using Interface Builder, it can be used to compare to the viewController parameter to see what tab was just selected.

@interface Pop2RootTabSwitchAppDelegate : NSObject 
    <UIApplicationDelegate, UITabBarControllerDelegate> {
  UINavigationController *tab1NavController;
}
@property (nonatomic, retain) IBOutlet UINavigationController *tab1NavController;
@end

@implementation Pop2RootTabSwitchAppDelegate

- (void)tabBarController:(UITabBarController *)tabBarController 
  didSelectViewController:(UIViewController *)viewController {
   NSLog(@"[%@ tabBarController:%@  didSelectViewController:%@]", [self class], 
       tabBarController, viewController);
   if (viewController == tab1NavController) {
       NSLog(@"viewController == tab1NavController");
       [tab1NavController popToRootViewControllerAnimated:NO];
   }
}

这篇关于问题弹出到标签栏开关上的根导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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