UINavigationController练习
2016-01-31 18:38
441 查看
#import "AppDelegate.h" #import <UIKit/UIKit.h> #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { float sysVersion=[[UIDevice currentDevice]systemVersion].floatValue; if (sysVersion>=8.0) { UIUserNotificationType type=UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound; UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:nil]; [[UIApplication sharedApplication]registerUserNotificationSettings:setting]; } UIWindow* w = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; w.backgroundColor = [UIColor whiteColor]; self.window = w; RootViewController* r =[[RootViewController alloc] init]; UINavigationController* navBar = [[UINavigationController alloc] initWithRootViewController:r]; w.rootViewController = navBar; [w makeKeyAndVisible]; return YES; }
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h" #import "SecondViewController.h" @implementation RootViewController -(void)viewWillAppear:(BOOL)animated { self.navigationController.navigationBar.barStyle = UIBarStyleBlack; [self.navigationController setToolbarHidden:NO animated:YES]; } -(void)viewDidLoad { [super viewDidLoad]; UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setFrame:CGRectMake(20, 64, 80, 40)]; [btn setBackgroundColor:[UIColor brownColor]]; [btn setTitle:@"添加" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; UIBarButtonItem* BarBtn = [[UIBarButtonItem alloc] initWithTitle:@"随机背景" style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)]; self.navigationItem.leftBarButtonItem = BarBtn; @try { UINavigationItem* item = self.navigationController.navigationBar.items[0]; NSLog(@"%p",self.navigationItem); NSLog(@"%p",item); item.title = @"HelloWorld"; } @catch (NSException *exception) { NSLog(@"%@",exception.description); } @finally { } } -(void)onClick:(UIBarButtonItem*) ABarBtn { CGFloat r = ( arc4random() % 256 / 256.0 ); CGFloat g = ( arc4random() % 256 / 256.0 ); CGFloat b = ( arc4random() % 256 / 256.0 ); UIColor* c2 = [UIColor colorWithRed:r green:g blue:b alpha:1]; self.view.backgroundColor = c2; } -(void)BtnClicked:(UIButton*)abtn { SecondViewController* sec = [[SecondViewController alloc] init]; [self.navigationController pushViewController:sec animated:NO]; } @end
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
#import "SecondViewController.h" @implementation SecondViewController -(void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blueColor]; } -(void)viewWillAppear:(BOOL)animated { UINavigationItem* item = self.navigationController.navigationBar.items[1]; [self.navigationController setToolbarHidden:YES animated:YES]; item.title = @"HelloWorldSec"; } @end个人对这个ui组件使用的理解
1,虽然UINavigationController继承于UIViewController,但是UIViewController保留了访问导航的属性,这样方便了各自实例的通讯。
2,自己写的类,如果要继承于UIViewController的话,最好直接继承,如果继承UITabViewController,虽然也有UINavigationController属性,但访问不到,为nil.
3,UINavigationController是栈管理的方式。
4,UINavigationController会为每一个实例创建个UINavigationItem.通过self.navigationItem与self.navigationController.navigationBar.item[0]的访问一样.
5,对navigationBar的设置都是全局的,不管在显示哪个ViewController.
6,这个navigationBar与Controller的View坐标重叠,如果新建subview,并且要显示出来,那么就得要考虑navigationBar的默认高度44.
相关文章推荐
- 修改Request 中的数据
- RabbitMQ学习笔记
- Intergraph CADWorx (Plant, P&ID, Equipment, IP, SpecEditor) 2016 v16.0-ISO 1DVD
- java.sql.SQLException: General error, message from server: "Field 'ID' doesn't have a default value
- java.net.SocketException: Can't assign requested address
- busybox filesystem matrix-gui-2.0 undefined function json_encode()
- UItextView回收键盘的几种方式
- LeetCode Unique Binary Search Trees
- android-Building a Notification,Preserving Navigation when Starting an Activity
- Codeforces 67C Sequence of Balls 编辑距离 dp
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- iOS UITextField
- iOS8下的UIAlertContoller初探
- UE-9260使用说明1
- 联合国大楼UVA1605(Building for UN)
- Vawio Sequence (NYOJ 763 LIS变形)
- Build.gradle配置
- 通过StoryBoard创建UIViewController
- 技术杂记-改造具有监控功能的数据库连接池阿里Druid,支持simple-jndi,kettle