IOS页面跳转的方法

时间:2021-05-23 23:25:51

在页面跳转时,若想用pushViewController,则必须在AppDelegate设置UINavigationController,否则pushViewController无效。
并且用pushViewController从A跳往B后,在B页面自带“Back”按钮返回上一页。类似于

IOS页面跳转的方法

这样设置,在AppDelegate中

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 2     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
 3     UIViewController *controllerA = [[ViewController alloc]init];
 4     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controllerA];
 5     
 6     self.window.rootViewController = nav;
 7     [self.window makeKeyAndVisible];
 8     
 9     return YES;
10 }

在controllerA中的跳转页面方法中这样写

-(void)turnToOtherController{
    ControllerB *controllerB = [[ControllerB alloc]init];
    [self.navigationController pushViewController:controllerB animated:YES];
}

这样,导航条就会一直存在。若不想要导航条,那么在AppDelegate这样写

1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
3     UIViewController *controllerA = [[ViewController alloc]init];
4     self.window.rootViewController = controllerA;
5     [self.window makeKeyAndVisible];
6     
7     return YES;
8 }

而下面这种方法是通用的,无需设置rootViewController。

[self presentViewController:mainView animated:YES completion:nil];//controller自带的属性

下面的则需要

[self.navigationController pushViewController:mainView animated:YES];
//必须在AppDelegate设置UINavigationController,否则pushViewController无效。
//并且使用pushViewController后,在B页面,会自带一个“Back”按钮返回上一页。