Iphone 动态更改导航控制器的rootviewcontroller
我正在尝试更改Iphone 动态更改导航控制器的rootviewcontroller,iphone,ios,xcode,Iphone,Ios,Xcode,我正在尝试更改didfishlaunchwithoptions中NavigationController的RootViewController 但我不知道该怎么做 我也浏览了这个链接: 这是我在didfishlaunchingwithoptions中的代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self
didfishlaunchwithoptions
中NavigationController
的RootViewController
但我不知道该怎么做 我也浏览了这个链接:
这是我在
didfishlaunchingwithoptions
中的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *rootController=[[HomePageController alloc] initWithNibName:@"HomePageController" bundle:nil];
navigationController=[[UINavigationController alloc] initWithRootViewController:rootController];
// presentation=[[PresentationController alloc]initWithNibName:@"PresentationController" bundle:nil];
//
// navigationController=[[UINavigationController alloc]initWithRootViewController:presentation];
//
// presentationList=[[PresentationListController alloc]initWithNibName:@"PresentationListController" bundle:nil];
//
// UINavigationController *listnavigation = [[UINavigationController alloc] initWithRootViewController:presentationList];
//
// revealer=[[ZUUIRevealController alloc]initWithFrontViewController:navigationController rearViewController:listnavigation];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
现在我评论并运行应用程序来更改rootviewcontroller
。然而,这不是实际的方法
如有任何帮助,我们将不胜感激。请不要这样做:
[self.window addSubview:navigationController.view];
这样说:
self.window.rootViewController = navigationController;
与此相反:
[self.window addSubview:navigationController.view];
这样说:
self.window.rootViewController = navigationController;
导航控制器不关心其根视图控制器是什么类型的视图控制器,只要它是UIViewController的子类。因此,您可以使用指向UIViewController的指针,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = nil;
if (iWantHomePageController)
{
rootController = [[HomePageController alloc] initWithNibName:@"HomePageController" bundle:nil];
}
else if (iWantPresentationController)
{
rootController = [[PresentationController alloc] initWithNibName:@"PresentationController" bundle:nil];
}
else if (iWantPresentationListController)
{
rootController = [[PresentationListController alloc] initWithNibName:@"PresentationListController" bundle:nil];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
导航控制器不关心其根视图控制器是什么类型的视图控制器,只要它是UIViewController的子类。因此,您可以使用指向UIViewController的指针,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = nil;
if (iWantHomePageController)
{
rootController = [[HomePageController alloc] initWithNibName:@"HomePageController" bundle:nil];
}
else if (iWantPresentationController)
{
rootController = [[PresentationController alloc] initWithNibName:@"PresentationController" bundle:nil];
}
else if (iWantPresentationListController)
{
rootController = [[PresentationListController alloc] initWithNibName:@"PresentationListController" bundle:nil];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
这对我来说真的很好:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
/*
both *navigationController and *viewController are declared
as properties in the .h file
*/
[self setViewController:[[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]];
[self setNavigationController:[[[UINavigationController alloc] initWithRootViewController:self.viewController]autorelease]];
[self.window setRootViewController:[self navigationController]];
[self.window makeKeyAndVisible];
这对我来说真的很好:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
/*
both *navigationController and *viewController are declared
as properties in the .h file
*/
[self setViewController:[[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]];
[self setNavigationController:[[[UINavigationController alloc] initWithRootViewController:self.viewController]autorelease]];
[self.window setRootViewController:[self navigationController]];
[self.window makeKeyAndVisible];
你能更详细地解释一下吗?你所做的只是在窗口顶部添加navigationViewController的视图。。。那不是你想要的。在不久的将来,你会遇到一些问题,例如旋转。你能更详细地解释一下吗?你所做的只是在窗口顶部添加navigationViewController的视图。。。那不是你想要的。在不久的将来,您将遇到问题,例如旋转。我可以从ViewController访问以下变量:iWantHomePageController、iWantPresentationController吗?如果是,那么我可以相应地设置这些值,一旦设置了这些值,就会触发此事件:'didFinish..'?如果是的话,我就完了。那么请解释一下我能做到吗?如果是,那么怎么做呢?这些变量只是我编的,用来演示根据您选择的逻辑将根视图控制器更改为您想要的任何内容的能力。您说过要动态地设置它们,因此必须使用一些逻辑来决定根视图控制器应该是什么。因此,如果该逻辑确定您需要主页控制器,那么您可以将rootController设置为HomePageController,其余设置也一样。如果您希望能够从应用程序中的其他位置设置这些值,您需要找到一种不同的方法来尝试执行您想要的操作。didFinishLaunchingWithOptions仅在应用程序最初启动时调用,并且在应用程序打开时不会再次调用。我可以从ViewController访问以下变量:iWantHomePageController、iWantPresentationController吗?如果是,那么我可以相应地设置这些值,一旦设置了这些值,就会触发此事件:'didFinish..'?如果是的话,我就完了。那么请解释一下我能做到吗?如果是,那么怎么做呢?这些变量只是我编的,用来演示根据您选择的逻辑将根视图控制器更改为您想要的任何内容的能力。您说过要动态地设置它们,因此必须使用一些逻辑来决定根视图控制器应该是什么。因此,如果该逻辑确定您需要主页控制器,那么您可以将rootController设置为HomePageController,其余设置也一样。如果您希望能够从应用程序中的其他位置设置这些值,您需要找到一种不同的方法来尝试执行您想要的操作。didFinishLaunchingWithOptions仅在应用程序最初启动时调用,并且在应用程序打开时不会再次调用。thx代表您的答案。thx代表您的答案。thx代表您的答案。thx代表您的答案。thx代表您的答案。thx代表您的答案。thx代表您的答案。thx代表您的答案。thx代表您的答案。