UINavigationController练习

#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.


你可能感兴趣的:(ios,UINavigationBar)