浅谈 iOS设计之多视图-导航按钮 UIBarButtonItem切换视图的方法

首先创建一个工程 之后再工程中创建两个类( FirstViewController和 SecondViewController)
 
操作方法如下
1、在AppDelegate中创建根视图
AppDelegate.h

#import <UIKit/UIKit.h>
#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;
}
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.
}
 
 
@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)];
 
}

//返回的方法实现
-(void)backPage
{
    [self.navigationController popToRootViewControllerAnimated:YES];
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
@end
 
 
结果效果图
点击“Next”按钮项视图会切换到下一个视图,“Back”按钮视图会返回到上一个视图
 
 

转载于:https://www.cnblogs.com/guiyangxueyuan/p/5277006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值