UINavigationController的用法

不可以直接用pushViewController:animated:,要在***AppDelegate.m中先实例化一个UINavigationController


新建一个Empty项目后,在application:didFinishLaunchingWithOptions:中的代码如下

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

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}



要在其中加入实例化一个UINavigationController的代码

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

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    

    FirstController *controller = [[FirstController alloc] init];

    UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];

    [controller release];

    self.window.rootViewController = navigationController;

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}


self.window.rootViewController = navigationController;

[self.window addSubview:navigationController.view];的区别:

前者是新的用法,在ios4之后新出现,后者是旧的写法。



这个应用的生命周期就只有一个navigationController,如果在用push进来的各个ViewController里的self.navigationController都是指同一个navigationController。




隐藏导航栏: 

navigationController.navigationBarHidden =YES; (推荐用这个)

或navigationController.navigationBar.hidden = YES;(不推荐用这个,因为如果把hidden设成YES,导航栏隐藏掉,再把hidden设成NO,导航栏并没有显示出来,用前一句代码不会出现此问题)

如果显示了导航栏,那么所有的ViewController的view在屏幕中都下移了一个导航栏的高度,因此如果把控件放在view的最底部,有可能会被遮住。为了预防这个问题,在xib的"Top Bar"的值设为"Navigation Bar",这样的好处是提醒实际运行中是有一个导航栏的,如果设置了这个,self.view.frame.size.height的值也变小了,这时是416(因为480-20-44=416)。





ViewController导航栏的标题: 

self.navigationItem.title =@"标题";


ViewController导航栏的view:

self.navigationItem.titleView = view;


ViewController导航栏隐藏返回按钮:

 self.navigationItem.hidesBackButton = YES;


ViewController导航栏的prompt:

self.navigationItem.prompt  =@"prompt test";





ViewController导航栏的左右按钮:

UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonSystemItemCanceltarget:selfaction:@selector(clickBack:)];

self.navigationItem.leftBarButtonItem = leftButton;

[leftButton release];

    

UIBarButtonItem *rightButton = [[UIBarButtonItemalloc]initWithTitle:@"确认"style:UIBarButtonSystemItemSavetarget:selfaction:@selector(clickSure:)];

self.navigationItem.rightBarButtonItem = rightButton;

[rightButton release];



说说以下这两句的区别

self.title = @"title_0000";

self.navigationItem.title = @"ttitle_1111";

实际上导航栏的标题只会显示 self.navigationItem.title的值,但如果我们不给self.navigationItem.title赋值,那为什么导航栏会显示self.title的值呢,因为当self.navigationItem.title没值时,会自动把self.title的值赋self.navigationItem.title。




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值