以编程方式构建/导航导航控制器

发布于 2024-12-02 05:29:00 字数 625 浏览 1 评论 0原文

之前:

我的应用程序基于独立的视图控制器。我可以通过替换应用程序委托上的根视图控制器来从一种切换到另一种:

ade.window.rootViewController = newController;

...并且到目前为止一切正常。

明天:

我们必须在我们的应用程序中添加一个基于 NavigationController 的部分,这将帮助用户导航我们的:

Brands =>型号名称 =>颜色

因此,用户将选择一种颜色,然后单击一个按钮:现在我将切换到另一个 UIViewController(称之为“pippo”),它实际上位于导航层次结构之外(我无法将其推送到导航控制器中)几种方法,我被迫这样做!)。

我想要的是从“pippo”回到我的“彩色”屏幕。因此,我正在寻找一种以编程方式“导航”我恢复的导航控制器的方法,我的意思是:

  • 我恢复我的导航控制器

  • 现在我在品牌上,但我不希望我的用户在这里,我想向他们展示他们上次使用的颜色(我将其保存在首选项中)

  • 如何模拟选择已知品牌和型号?

多谢。

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:

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反目相谮 2024-12-09 05:29:00

在应用程序委托的 applicationDidFinishLoading 中:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

[window makeKeyAndVisible];
[window addSubview:navController.view];

这将实例化导航控制器并将其作为视图添加到窗口中。

现在,在您的 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];
}

在您的 SecondViewController 中,您可以使用以下命令向后导航堆栈:

- (void)clickedAnotherButton:(id)selector {
  // goes back to the last view controller in the stack
  [self.navigationController popViewControllerAnimated:YES];
}

所以对您来说,它会go:

在应用程序委托中设置导航控制器,并将 Brand 作为根视图控制器
用户选择他们的品牌,然后您 pushViewController:animated: Model 视图控制器。然后用户选择他们的模型,然后您 pushViewController:animated: Color 视图控制器。类似地,用户选择一种颜色,然后您按下 Pippo 视图控制器。现在,如果用户按回键(或者调用 popViewControllerAnimated:),它将返回到 Color 视图控制器,其状态与用户离开它时的状态相同Pippo 控制器。

In applicationDidFinishLoading in App delegate:

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:

- (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:

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

荒芜了季节 2024-12-09 05:29:00

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

Write the following code in AppDelegate.m Class

- (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];
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文