简体   繁体   中英

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

I am an iOS development newbie. I want to go to another page (CountryViewController) from my current page (CityViewController) on the click of a button. 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. If so then you can create the VC and do this inside of your parent view controller.

    [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 -

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

In second method, I use this controller without UINavigationTabBar . Hide this navigationBar & provide custom buttons based on which [self.navigationController popViewControllerAnimated] should occur.

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

set storyboard id in ViewController2 "Identity inspector".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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