首先创建一个工程 之后再工程中创建两个类( FirstViewController和 SecondViewController)
操作方法如下
1、在AppDelegate中创建根视图
AppDelegate.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
#import "FirstViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *firstVC=[[FirstViewController alloc]init];
//创建导航视图
UINavigationController *naviGC=[[UINavigationController alloc]initWithRootViewController:firstVC];
//把导航视图作为根视图
self.window.rootViewController=naviGC;
return YES;
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *firstVC=[[FirstViewController alloc]init];
//创建导航视图
UINavigationController *naviGC=[[UINavigationController alloc]initWithRootViewController:firstVC];
//把导航视图作为根视图
self.window.rootViewController=naviGC;
return YES;
}
2、对两个分类进行操作
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
@end
FirstViewController.m
// 多视图--模态视图
//
// Created by scjy on 16/3/14.
// Copyright © 2016年 lanco. All rights reserved.
//
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主视图的背景色
self.view.backgroundColor=[UIColor blueColor];
//创建按钮项
UIBarButtonItem *BarBI=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
//按钮项的位置
self.navigationItem.rightBarButtonItem=BarBI;//位置在视图的右侧
}
//方法实现
-(void)nextPage
{
SecondViewController *secondVC=[[SecondViewController alloc]init];
//把视图推入到视图secondVC
[self.navigationController pushViewController:secondVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 多视图--模态视图
//
// Created by scjy on 16/3/14.
// Copyright © 2016年 lanco. All rights reserved.
//
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主视图的背景色
self.view.backgroundColor=[UIColor blueColor];
//创建按钮项
UIBarButtonItem *BarBI=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
//按钮项的位置
self.navigationItem.rightBarButtonItem=BarBI;//位置在视图的右侧
}
//方法实现
-(void)nextPage
{
SecondViewController *secondVC=[[SecondViewController alloc]init];
//把视图推入到视图secondVC
[self.navigationController pushViewController:secondVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主视图的背景色
self.view.backgroundColor=[UIColor greenColor];
//隐藏返回按钮
self.navigationItem.hidesBackButton=YES;
//添加返回按钮
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:2 target:self action:@selector(backPage)];
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置主视图的背景色
self.view.backgroundColor=[UIColor greenColor];
//隐藏返回按钮
self.navigationItem.hidesBackButton=YES;
//添加返回按钮
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:2 target:self action:@selector(backPage)];
}
//返回的方法实现
-(void)backPage
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//返回的方法实现
-(void)backPage
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
结果效果图
点击“Next”按钮项视图会切换到下一个视图,“Back”按钮视图会返回到上一个视图