简体   繁体   English

iOS - 如何在单击按钮时移动到其他页面

[英]iOS - how to move to a different page on click of a button

I am an iOS development newbie. 我是iOS开发新手。 I want to go to another page (CountryViewController) from my current page (CityViewController) on the click of a button. 我想通过单击按钮从当前页面(CityViewController)转到另一个页面(CountryViewController)。 How would I do that? 我该怎么办? Pardon me if this is a very beginner question. 请原谅我这是一个非常初学的问题。

There are several ways. 有几种方法。 I assume you are using a UINavigationController. 我假设您正在使用UINavigationController。 If so then you can create the VC and do this inside of your parent view controller. 如果是,那么您可以创建VC并在父视图控制器内执行此操作。

    [self.navigationController pushViewController:viewController animated:YES]

So basically you are trying to build a multi-view app. 所以基本上你正在尝试构建一个多视图应用程序。 There are many ways to do so. 有很多方法可以做到这一点。 Bu I'll list 3 common ones - 我会列出3个常见的 -

  1. [self.view insertSubview:newViewController.view atIndex:3];
  2. Using UINavigationController 使用UINavigationController
  3. Finally using modalViewController - [self presentModalViewController:newViewController animated:YES]; 最后使用modalViewController - [self presentModalViewController:newViewController animated:YES];

In second method, I use this controller without UINavigationTabBar . 在第二种方法中,我使用没有UINavigationTabBar控制器。 Hide this navigationBar & provide custom buttons based on which [self.navigationController popViewControllerAnimated] should occur. 隐藏此navigationBar并提供基于[self.navigationController popViewControllerAnimated]应该发生的自定义按钮。

ViewController2 *newView = [self.storyboard instantiateViewControllerWithIdentifier:@"viewController"];
[self.navigationController pushViewController:newView animated:YES];

set storyboard id in ViewController2 "Identity inspector". 在ViewController2“身份检查器”中设置故事板ID。

I have came across the same problem. 我遇到了同样的问题。 While User logged in once need to redirect to different page or else need to stay in homepage by default. 用户登录后需要重定向到不同的页面,否则默认情况下需要留在主页。

Here is the code snippet. 这是代码片段。

Here 这里

 N_loginmsg = @"success";
    NSString *N_loginmsg = [[NSUserDefaults standardUserDefaults]objectForKey:@"remember_loginmsg"];
        NSString *storyboardId;
        if (N_loginmsg != nil && [N_loginmsg isEqual:@"Success"])
        {
            storyboardId = @"ListViewController";
        }
        else
        {
            storyboardId = @"HomeViewController";
        }
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:storyboardId];
        UINavigationController *mynav = [[UINavigationController alloc]initWithRootViewController:initViewController];
        self.window.rootViewController = mynav;
        [self.window makeKeyAndVisible];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM