iOS开发从入门到精通--导航控制器基础

来源:互联网 发布:知乎日报和读读日报 编辑:程序博客网 时间:2024/05/16 17:41

导航控制器基础
这里写图片描述

实现上面的导航栏,左侧按钮,中间的TITLE,还有右侧的test按钮,和一个播放的按钮

重新创建一个VCRoot根视图控制器

#import "AppDelegate.h"#import "VCRoot.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    //创建一个根视图控制器    VCRoot * root = [[VCRoot alloc]init];    //创建导航控制器    //导航控制器主要用来管理多个视图控制器的切换    //层级的方式来管理多个视图控制器    //创建控制器时,一定要有一个根视图控制器    //参数一:就是作为导航控制器的根视图控制器    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];    //window的根视图设置为导航控制器    self.window.rootViewController =nav;    [self.window makeKeyAndVisible];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

然后:

#import "VCRoot.h"@interface VCRoot ()@end@implementation VCRoot- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor=[UIColor yellowColor];    //设置导航栏的标题文字    self.title =@"根视图";    //设置导航元素项目的标题    //如果没有设置navigationItem.title,为nil    //系统会使用 self.title作为标题    //如果self.navigationItem.titl不为空    //将self.navigationItem.titl设置为标题内容    self.navigationItem.title=@"TITLE";    //创建一个导航栏左侧的按钮    //根据title文字来创建按钮    //p1:按钮上的文字    //p2:按钮风格    //p3:事件拥有者    //p4:按钮事件    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarButtonItemStyleDone target:self action:@selector(pressLeft)];    //将导航元素项的左侧按钮赋值    self.navigationItem.leftBarButtonItem=leftBtn;    //创建一个导航栏右侧的按钮    //根据系统风格来创建按钮    //只需要指定风格样式,系统风格的按钮内容或标题文字不能改变    //p1:按钮风格UIBarButtonSystemItemAdd/UIBarButtonSystemItemPlay....    //p2:事件拥有者    //p3:按钮事件    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(pressRight)];    self.navigationItem.rightBarButtonItem=rightBtn;    //标签对象    UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 40)];    label.text=@"test";    label.textAlignment=NSTextAlignmentCenter;    //将任何类型的控件添加到导航按钮的方法    UIBarButtonItem * item03 = [[UIBarButtonItem alloc]initWithCustomView:label];    //创建按钮数组    NSArray * arrayBtn = [NSArray arrayWithObjects:rightBtn,item03, nil];    //将右侧按钮数组赋值    self.navigationItem.rightBarButtonItems=arrayBtn;}-(void) pressLeft{    NSLog(@"左侧按钮被按下!");}-(void) pressRight{    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
0 0
原创粉丝点击