国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

IOS之導(dǎo)航控制器

2019-11-14 18:07:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  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];

效果圖如下:

 

作者:杰瑞教育
出處:http://www.survivalescaperooms.com/jerehedu/ 
版權(quán)聲明:本文版權(quán)歸臺(tái)杰瑞教育技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
技術(shù)咨詢:JRedu技術(shù)交流
 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 合江县| 若尔盖县| 德庆县| 鲁山县| 金川县| 城步| 宿松县| 松桃| 忻城县| 耒阳市| 当涂县| 定边县| 九龙坡区| 乐昌市| 策勒县| 淮滨县| 湄潭县| 铜陵市| 邛崃市| 清新县| 西贡区| 荔浦县| 鹿邑县| 平定县| 平潭县| 舞阳县| 南城县| 建平县| 阿克苏市| 赤峰市| 大田县| 香港| 高碑店市| 华坪县| 安仁县| 临朐县| 扶余县| 阿尔山市| 奇台县| 龙口市| 永顺县|