问题弹出到标签栏开关上的根导航控制器

时间:2021-08-25 17:27:25

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.

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

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.)

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

Any hints? IPD

任何提示? 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];
}

1 个解决方案

#1


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.

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

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.

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

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.

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

@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];
   }
}

#1


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.

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

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.

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

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.

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

@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];
   }
}