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。




你可能感兴趣的:(Objective-C)