iOS 切换选项卡的子视图控制器
iOS switch tab's child view controller
有没有办法以编程方式切换某个选项卡的视图?例如,假设我有一个包含 3 个选项卡的选项卡栏。第三个选项卡包含一个 login/registration 流程,完成后应在同一个第三个选项卡中显示帐户屏幕。
大致如下:
if (authenticated) {
// Set TabBarController tab 3 view to AccountController
} else {
// Set TabBarController tab 3 view to LoginController
}
您可以在选项卡内使用 UINavigationController,然后在注册完成后,只需在其中推送一个新的 viewController。
您可以使用 UITabBarController method:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
默认行为如下:
When you assign a new set of view controllers at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. When changing the view controllers, the tab bar controller remembers the view controller object that was previously selected and attempts to reselect it. If the selected view controller is no longer present, it attempts to select the view controller at the same index in the array as the previous selection. If that index is invalid, it selects the view controller at index 0.
另外,UITabBarController 有
@property(nonatomic, copy) NSArray *viewControllers
具有相同的行为,但默认情况下没有动画。
无论如何,您应该重新创建所有 ViewController 的层次结构。并且不要忘记 UITabBarController 会记住之前选择的视图控制器对象并尝试重新选择它或回退到 0 索引。
有没有办法以编程方式切换某个选项卡的视图?例如,假设我有一个包含 3 个选项卡的选项卡栏。第三个选项卡包含一个 login/registration 流程,完成后应在同一个第三个选项卡中显示帐户屏幕。
大致如下:
if (authenticated) {
// Set TabBarController tab 3 view to AccountController
} else {
// Set TabBarController tab 3 view to LoginController
}
您可以在选项卡内使用 UINavigationController,然后在注册完成后,只需在其中推送一个新的 viewController。
您可以使用 UITabBarController method:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
默认行为如下:
When you assign a new set of view controllers at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. When changing the view controllers, the tab bar controller remembers the view controller object that was previously selected and attempts to reselect it. If the selected view controller is no longer present, it attempts to select the view controller at the same index in the array as the previous selection. If that index is invalid, it selects the view controller at index 0.
另外,UITabBarController 有
@property(nonatomic, copy) NSArray *viewControllers
具有相同的行为,但默认情况下没有动画。
无论如何,您应该重新创建所有 ViewController 的层次结构。并且不要忘记 UITabBarController 会记住之前选择的视图控制器对象并尝试重新选择它或回退到 0 索引。