您的位置:首页 > 产品设计 > UI/UE

IOS开发-UI学习-UINavigationController(导航控制器)的使用

2016-04-05 18:19 459 查看
UINavigationController是IOS 中常用的功能,基本用法如下:

1、在AppDelegate.m中添加如下代码:

#import "AppDelegate.h"
#import "MainViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//   注意:如果保留storyboard中的viewcontroller的话,就不用第16行到20行的创建window的语句

//    创建window,设置背景色
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor colorWithRed:0.638 green:0.876 blue:1.000 alpha:1.000];
//    让当前window称为主窗口
[self.window makeKeyAndVisible];

//    设置window的根视图
MainViewController *mainVC = [[MainViewController alloc]init];

//    设置导航控制器的根视图为mainviewcontroller类的实例mainVC
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:mainVC];

//    设置window的根视图为nav
self.window.rootViewController = nav;

//    给导航栏着色
nav.navigationBar.barTintColor = [UIColor colorWithRed:1.000 green:0.400 blue:1.000 alpha:1.000];

//    给导航栏添加图片
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"barimage"] forBarMetrics:UIBarMetricsDefault];

return YES;
}


注意:使用以上功能时先在Main.storyboard中删除viewcontroller,然后添加十六到二十行语句,如果不删除Main.storyboard中的viewcontroller的话,就不需要使用十六到二十行多语句来添加window。

新建MainViewController类,继承自UIViewController,然后在MainViewController.m中添加以下代码:

#import "MainViewController.h"
#import "FirstViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
[super viewDidLoad];

//    设置标题
//    self.navigationItem.title = @"微信";

//    设置按钮
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
btn1.backgroundColor = [UIColor redColor];
[btn1 addTarget:self action:@selector(gotofirst) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];

//    自定义标题(按钮、可点击)
UIButton *titlebtn = [UIButton buttonWithType:UIButtonTypeCustom];
titlebtn.frame = CGRectMake(0, 0, 100, 44);
[titlebtn setTitle:@"我可以点击" forState: UIControlStateNormal];
[titlebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
titlebtn.titleLabel.font = [UIFont systemFontOfSize:16];
[titlebtn addTarget:self action:@selector(titlebtnAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titlebtn;

//    自定义左、右按键
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"左边" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction)];

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"右边" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction)];

//    self.navigationItem.leftBarButtonItem = leftItem;
//    self.navigationItem.rightBarButtonItem = rightItem;

//    使用数组给右边添加多个按钮
self.navigationItem.rightBarButtonItems = @[leftItem,rightItem];

}

-(void)rightItemAction{
NSLog(@"右边按钮被点击了");
}

-(void)leftItemAction{
NSLog(@"左边按钮被点击了");
}

-(void)gotofirst{
FirstViewController *firstVC = [[FirstViewController alloc]init];
[self.navigationController pushViewController:firstVC animated:YES];

}
-(void)titlebtnAction{
NSLog(@"我被点击了");
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end


新建FirstViewController类,然后在FirstViewController.m中添加以下代码:

#import "FirstViewController.h"
#import "MainViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor blackColor];

//    设置按钮
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
btn1.backgroundColor = [UIColor redColor];
[btn1 addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}

-(void)goback{

//按指定返回
////    返回根视图
//    [self.navigationController popToRootViewControllerAnimated:YES];
//
////    返回上一个视图
//    [self.navigationController popViewControllerAnimated:YES];

//通过循环比较返回
for (UIViewController *tmp in self.navigationController.viewControllers) {
if ([tmp isKindOfClass:[MainViewController class]]) {
[self.navigationController popToViewController:tmp animated:YES];
}
}

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end


通过以上代码就可以实现两个viewcontroller的切换,是通过导航控制器实现的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: