UINavigationController是用于構(gòu)建分層應(yīng)用程序的主要工具,主要采用棧形式來(lái)實(shí)現(xiàn)視圖。任何類(lèi)型的視圖控制器都可放入棧中。在設(shè)計(jì)導(dǎo)航控制器時(shí)需要指定根視圖即用戶看到的第一個(gè)視圖。根視圖控制器是被導(dǎo)航控制器推入到棧中的第一個(gè)視圖控制器。當(dāng)用戶查看下一個(gè)試圖時(shí),棧中將加入一個(gè)新的視圖控制器,它所控制的視圖將展示給用戶。我們可以通過(guò)導(dǎo)航按鈕來(lái)操作分層的應(yīng)用程序,用它來(lái)控制視圖的推入或推出。
1、把子控制器添加到導(dǎo)航控制器中常用的方法
//創(chuàng)建視圖控制器 JRViewController * vc=[[JRViewController alloc] init]; //創(chuàng)建導(dǎo)航控制器,并且將上面的控制器作為導(dǎo)航控制器的根控制器 UINavigationController * naVC=[[UINavigationController alloc] initWithRootViewController:vc]; //將當(dāng)前的導(dǎo)航控制器設(shè)置窗口的根視圖控制器self.window.rootViewController=naVC;
2、這樣我們就把JRViewController作為了當(dāng)前導(dǎo)航控制器的根控制器,下面我們進(jìn)入到JRViewController,并且添加一個(gè)標(biāo)題和按鈕代碼如下
//設(shè)置背景 self.view.backgroundColor=[UIColor redColor]; //設(shè)置標(biāo)題 self.title=@"精品"; //添加按鈕 UIButton * button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 45)]; button.backgroundColor=[UIColor blackColor]; [button setTitle:@"push" forState:UIControlStateNormal]; button.center=self.view.center; [button addTarget:self action:@selector(pushVCNew) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
效果圖如下:
3、下面我們就進(jìn)行視圖控制器之間的跳轉(zhuǎn)
導(dǎo)航控制器管理視圖控制器主要采取壓棧和出棧的方式,下面我們一起來(lái)感受一下如何切換,上面添加了按鈕我們?cè)黾恿它c(diǎn)擊事件,下面我們重新一下這個(gè)點(diǎn)擊事件觸發(fā)的方法:
- (void) pushVCNew{ //初始化第二個(gè)控制器 SecondViewController * sec=[[SecondViewController alloc] init]; //切換到另一個(gè)視圖控制器 [self.navigationController pushViewController:sec animated:YES]; }
效果圖如下:
壓棧進(jìn)入后響應(yīng)的就有出棧,如何進(jìn)行出棧返回呢,我們重寫(xiě)一下第二個(gè)控制器的返回方法,調(diào)用控制器的popViewControllerAnimated方法即可
- (void) popVC{ [self.navigationController popViewControllerAnimated:YES];}
5、導(dǎo)航按鈕
//1 構(gòu)造方法 UIBarButtonItem * item1=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bt1"] style:UIBarButtonItemStylePlain target:self action:@selector(leftClick)]; //2 自定義視圖 UIButton * button=[[UIButton alloc] initWithFrame:CGRectMake(100, 100, 50, 40)]; button.backgroundColor=[UIColor redColor]; [button setTitle:@"hello" forState:UIControlStateNormal]; UIBarButtonItem * item2=[[UIBarButtonItem alloc] initWithCustomView:button]; //3 添加到當(dāng)前navigationItem self.navigationItem.leftBarButtonItem=item2; self.navigationItem.rightBarButtonItem=item1; //4 增加標(biāo)題 UILabel * label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)]; label.text=@"導(dǎo)航控制器"; label.textColor=[UIColor redColor];self.navigationItem.titleView=label;
效果圖如下
6、ToolBar工具條,每個(gè)導(dǎo)航控制器都有一個(gè)工具條,默認(rèn)為隱藏狀態(tài),下面我們將工具條打開(kāi)并使用它。
//打開(kāi)toolbar self.navigationController.toolbarHidden=NO; //定義ToolBar增加按鈕 UIBarButtonItem * item1=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pushVC)]; UIBarButtonItem * item2=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(pushVC)]; UIBarButtonItem * item3=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(pushVC)]; UIBarButtonItem * item4=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(pushVC)]; UIBarButtonItem * item5=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pushVC)]; UIBarButtonItem * item6=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(pushVC)]; UIBarButtonItem * item7=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(pushVC)]; UIBarButtonItem * item8=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(pushVC)]; item8.width=20; //將按鈕添加到工具條 self.toolbarItems=@[item1,item8,item2,item8,item3,item8,item4,item7,item5,item8,item6];
效果圖如下:
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注