ios 以编程方式构建/导航导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7241923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically build / navigate a Navigation Controller
提问by Fabio B.
Before:
前:
My App is based on indepent view controllers. I can switch from one to another by replacing the root view controller on the application delegate:
我的应用程序基于独立的视图控制器。我可以通过替换应用程序委托上的根视图控制器来从一个切换到另一个:
ade.window.rootViewController = newController;
... and all worked right, till now.
...一切正常,直到现在。
Tomorrow:
明天:
we have to add a NavigationController-based part of our App, which will help the users navigate through our:
我们必须在我们的应用程序中添加一个基于 NavigationController 的部分,这将帮助用户浏览我们的:
Brands => Model Names => Colors
品牌 => 型号名称 => 颜色
So, the user will choose a color, then click a button: now I will switch to another UIViewController (call it "pippo"), which actually resides outside that navigation hierarchy (I can't push it in the nav-controller for several methods, I'm forced doing so!).
因此,用户将选择一种颜色,然后单击一个按钮:现在我将切换到另一个 UIViewController(称之为“pippo”),它实际上驻留在该导航层次结构之外(我无法将它推入导航控制器中的几个方法,我被迫这样做!)。
What I want is to get back to my "Color" screen, from "pippo". So, I'm looking for a way to programmatically "navigate" the navigation controller I restore, I mean:
我想要的是从“pippo”回到我的“彩色”屏幕。所以,我正在寻找一种以编程方式“导航”我恢复的导航控制器的方法,我的意思是:
I restore my navigation controller
now I'm on Brands, but I don't want my users to be here, I want to show them the last color they was on (I saved it in the preferences)
how can I simulate the selection of a known brand and model?
我恢复了我的导航控制器
现在我在使用品牌,但我不希望我的用户在这里,我想向他们展示他们使用的最后一种颜色(我将其保存在首选项中)
如何模拟已知品牌和型号的选择?
Thanks a lot.
非常感谢。
回答by Thomas Clayson
In applicationDidFinishLoading
in App delegate:
在applicationDidFinishLoading
应用程序委托中:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[window makeKeyAndVisible];
[window addSubview:navController.view];
That will instantiate the navigation controller and add it to the window as a view.
这将实例化导航控制器并将其作为视图添加到窗口中。
Now, in your rootViewController class (lets say its called FirstViewController
) you can do this:
现在,在您的 rootViewController 类(可以说它被称为FirstViewController
)中,您可以执行以下操作:
- (void)clickedAButton:(id)selector {
SecondViewController *nextViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
// and push it onto the 'navigation stack'
[self.navigationController pushNavigationController:nextViewController animated:YES];
// and release
[nextViewController release];
}
And in your SecondViewController
you can navigate back through the stack using:
而在你的SecondViewController
,你可以通过使用堆栈向后导航:
- (void)clickedAnotherButton:(id)selector {
// goes back to the last view controller in the stack
[self.navigationController popViewControllerAnimated:YES];
}
So for you it would go:
所以对你来说它会去:
Set up navigation controller in the app delegate with Brand
as the root view controller
User chooses their brand and you pushViewController:animated:
the Model
view controller. Then the user chooses their model and you pushViewController:animated:
the Color
view controller. Similarly the user chooses a color and you push the Pippo
view controller. Now, if the user presses back (or you call popViewControllerAnimated:
) it will go back to the Color
view controller in the same state as when the user left it to go to the Pippo
controller.
成立于应用程序的委托导航控制器Brand
的根视图控制器用户选择自己的品牌和你pushViewController:animated:
的Model
视图控制器。然后,用户选择他们的模型和你pushViewController:animated:
的Color
视图控制器。类似地,用户选择一种颜色,然后按下Pippo
视图控制器。现在,如果用户按回(或您调用popViewControllerAnimated:
),它将返回到Color
视图控制器,其状态与用户离开它转到Pippo
控制器时的状态相同。
回答by user2998756
Write the following code in AppDelegate.m Class
在 AppDelegate.m 类中编写如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
self.nav = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.nav.navigationBarHidden = YES;
[mainViewController release];
[_window addSubview:nav.view];
[_window makeKeyAndVisible];
}