我有一个以modalview呈现的navigationController,其rootviewcontroller是FirstViewController.At某点我想将navigationController的rootviewcontroller更改为SecondViewController。我做的是
[self.navigationController initWithRootViewController:SecondViewController];
我不确定我所做的是否正确以及FirstViewController是否已发布?请问有谁知道这样做的正确方法是什么?
提前致谢!
答案 0 :(得分:41)
要么
[firstViewController.navigationController setViewControllers: [NSArray arrayWithObject: secondViewController]
animated: YES];
或
firstViewController.navigationController.viewControllers = [NSArray arrayWithObject: secondViewController];
其中firstViewController
是FirstViewController
的实例,secondViewController
分别是SecondViewController
类的实例。后一种变体是setViewControllers:animated:
没有动画的快捷方式。
答案 1 :(得分:1)
- (void) changeRootViewControllerOFNavigationControlllerAtRuntime:(UIViewController *) viewController {
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:viewController];
[UIApplication sharedApplication].delegate.window.rootViewController=navController;
}
答案 2 :(得分:0)
这不是正确的方法,在已经初始化的对象上调用init
很少(我认为永远不会)。
我解决这个问题的方法是创建UINavigationController的子类。
在这个子类中,我覆盖了initwithrootviewcontroller:
- (id) initWithRootViewController:(UIViewController *)rootViewController
{
UIViewController *fakeController = [[[UIViewController alloc] init] autorelease];
self = [super initWithRootViewController:fakeController];
if(self)
{
self.fakeRootViewController = fakeController;
rootViewController.navigationItem.hidesBackButton = YES;
[self pushViewController:rootViewController animated:NO];
}
return self;
}
fakeRootViewController实际上什么都不做,这是iOS的一种解决方法,无法设置rootviewcontroller。
在另一个函数(setRootViewController:aViewController)中,您隐藏了新的“rootviewcontroller”的后退按钮,因此用户永远不会看到有一个假的rootviewcontroller。然后将它推到fakerootviewcontroller上面
应该覆盖poptorootviewcontroller,以确保它始终弹出到堆栈的索引1,而不是索引0。
应该更改viewcontrollers的getter,这样它就会返回一个不带fakerootviewcontroller的数组(removeobjectatindex: 0
)
希望这有帮助!
答案 3 :(得分:0)
您需要制作自定义UINavigationController
@interface mySwitchRootViewNavigationController()
@property (nonatomic, retain) myFirstViewController * FirstViewController;
@property (nonatomic, retain) mySecondViewController * SecondViewController;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.FirstViewController = [[myFirstViewController alloc] init];
self.SecondViewController = [[mySecondViewController alloc] init];
}
-(void) setRootViewControllerWithID:(int) viewControllerID
{
if (viewControllerID == 1) {
self.viewControllers = [NSArray arrayWithObject:self.SecondViewController];
} else
{
self.viewControllers = [NSArray arrayWithObject:self.FirstViewController];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[self setRootViewControllerWithID:intVar];
[super viewWillAppear:animated];
}
初始化
mySwitchRootViewNavigationController * switchView = [mySwitchRootViewNavigationController alloc] init];
答案 4 :(得分:0)
Swift 3
fileprivate func changeRootVC() {
if let newVC = self.storyboard?.instantiateViewController(withIdentifier: "MyStoryboardID"), let nc = self.navigationController {
nc.setViewControllers([newVC], animated: true)
}
}