UIViewController有兩個常用屬性,一個控制導航顯示內容,一個是自身導航控制器
@property(nonatomic,readonly,retain)UINavigationItem *navigationItem;// Created on-demand so that a view controller may customize its navigation appearance.
@property(nonatomic,readonly,retain)UINavigationController *navigationController;// If this view controller has been pushed onto a navigation controller, return it.
UINavigationItem的常用屬性
@property(nonatomic,copy) NSString *title; // Title when topmost on the stack. default is nil 導航視圖控制器標題
@property(nonatomic,retain)UIBarButtonItem *leftBarButtonItem; // 左導航鍵
@property(nonatomic,retain)UIBarButtonItem *rightBarButtonItem; //右導航鍵
UIBarButtonItem常用構造方法
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
UINavigationController繼承自UIViewController
一般用法是創建一個UINavigationController將UIViewController設置爲它的根視圖來顯示
常用屬性
@property(nonatomic,readonly)UINavigationBar *navigationBar;// The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.
// 常常將它的屬性 translucent 設置爲NO來控制導航欄不透明,此時座標原點移動到導航欄下方視圖的左上角
UINavigationBar 常用屬性
@property(nonatomic,assign,getter=isTranslucent)BOOL translucent NS_AVAILABLE_IOS(3_0);// Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
示例
AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
FirstViewController * firstViewController = [[FirstViewControlleralloc]init];
UINavigationController * nav = [[UINavigationControlleralloc]initWithRootViewController:firstViewController];
[self.windowsetRootViewController:nav];
[self.windowmakeKeyAndVisible];
return YES;
}
FirstViewController.m中
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
//設置導航標題和導航右鍵
[self.navigationItemsetTitle:@"第一視圖"];
[self.navigationController.navigationBarsetTranslucent:NO];//設置導航欄不透明
UIBarButtonItem * rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"next"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(btnAction)];
[self.navigationItemsetRightBarButtonItem:rightBarButtonItem];
[rightBarButtonItem release];
//測試按鈕
UIButton * btn = [[UIButtonalloc]initWithFrame:CGRectMake(60,200,200,50)];
[btn setBackgroundColor:[UIColorgrayColor]];
[btn setTitle:@"進入下一視圖控制器" forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(btnAction)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
}
-(void)btnAction
{
SecondViewController * secondViewController = [[SecondViewControlleralloc]init];
//模態進入
//UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//[self presentViewController:nav animated:NO completion:^{}];
//[secondViewController release];
//[nav release];
//非模態進入
[self.navigationControllerpushViewController:secondViewControlleranimated:NO];
[secondViewController release];
}
SecondViewController中
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
//設置導航標題和導航左鍵
[self.navigationItemsetTitle:@"第二視圖"];
UIBarButtonItem * leftBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"previous"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(btnAction)];
[self.navigationItemsetLeftBarButtonItem:leftBarButtonItem];
[leftBarButtonItem release];
//測試按鈕事件
UIButton * btn = [[UIButtonalloc]initWithFrame:CGRectMake(60,200,200,50)];
[btn setBackgroundColor:[UIColorgreenColor]];
[btn setTitle:@"返回上一視圖控制器" forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(btnAction)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
}
-(void)btnAction
{
//模態返回
//[self dismissViewControllerAnimated:NO completion:^{}];
//非模態返回
[self.navigationControllerpopViewControllerAnimated:NO];
}