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
- iOS开发从入门到精通--导航控制器基础
- iOS开发从入门到精通--基础组件UIButton基础
- iOS开发从入门到精通--基础组件UILabel
- iOS开发从入门到精通--UIView基础
- iOS开发从入门到精通--UIWindow基础
- iOS开发从入门到精通--UIViewController基础
- iOS开发从入门到精通--UIGesture手势基础
- iOS开发从入门到精通-- 步进器UIStepper和分栏控制器UISegmentedControl
- 4-读书笔记----iOS开发指南:从零基础到App Store上架--iOS视图控制器与导航模式
- VC++游戏开发基础系列从入门到精通
- VC++游戏开发基础系列从入门到精通
- Nginx开发从入门到精通-基础篇
- iOS开发从入门到精通--UIButton事件处理
- iOS开发从入门到精通--UIView层级关系
- iOS开发从入门到精通--UIViewController使用
- iOS开发从入门到精通--开关按钮UISwitch控件
- iOS开发从入门到精通-- 单行文本UITextField
- iOS开发从入门到精通-- 滚动视图UIScrollView
- @media screen针对不同移动设备-响应式设计
- 服务器和客户端 tcp通信
- java的基本数据类型有哪些java的八大基本数据类型
- 求2^n-char 数组表示超大整数
- OC(一)类、对象、属性、成员变量、方法(整理)
- iOS开发从入门到精通--导航控制器基础
- 让DIV内部内容撑开外层的div方法
- 【Life】 你为什么会离开游戏行业?
- poj 2406 Power Strings kmp next数组
- liunx安装httpd服务
- 只使用I/O的输入单数字的函数,实现任意实数的输出
- 字符串转十六进制字符函数
- 线程-事件机制
- 被解放的GPU与CSS3中对GPU的应用分析