18HWThread_UINavigationController_Title_Button_Hidden_UseStoryBoard

大綱

一剩蟀、作業(yè)Homework_CircleMove0314
理論:不可在分線程中刷新UI,否則無法實現(xiàn)預期界面效果切威。

二育特、UINavigationController
項目:UINavigationController0315
理論:
棧:后進先出 隊列:先進先出

導航(NavController):繼承于UIViewController,根據(jù)棧的原理先朦,實現(xiàn)多個視圖的之間的切換功能缰冤。

界面跳轉(zhuǎn)方法共4種:①root②模態(tài)彈出③
④UINavigationController:
步驟:
1.創(chuàng)建ViewController
2.創(chuàng)建NavigationController,且設置vc為nav的root
3.將nav設置為window的root
進棧:pushViewController: animated:
出棧:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController

三喳魏、Navigation_Title_Button
項目:Navigation_Title_Button0315
理論:
1.顯示導航欄標題
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title

2.添加導航按鈕
2.1 系統(tǒng)提供的方式
①initWithBarButtonSystemItem: target: action:
②initWithTitle: style: target: action:
③initWithImage: style: target: action:
2.2 自定義方式
步驟:
1.創(chuàng)建button棉浸,設置其bounds/action等
2.聲明UIBarButtonItem,并initWithCustomView:button
3.將button添加到nav

四截酷、隱藏導航欄
項目:NavigationBar0315
理論:
設置導航隱藏:self.navigationController.navigationBarHidden = YES;
位置:一般設置在viewWillAppear:

五、使用storyBoard創(chuàng)建導航欄
項目:Navigation_UseStoryBoard0315
理論:
1.選中storyboard
2.點擊Editor→Embed In→Navigation Controller

正文

一乾戏、作業(yè)Homework_CircleMove0314
理論:不可在分線程中刷新UI迂苛,否則無法實現(xiàn)預期界面效果。
源碼:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //1.在圓心添加太陽
    UIImageView *sun = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fireball"]];
    sun.center = CGPointMake(CENTER_X, CENTER_Y);
    sun.bounds = CGRectMake(0, 0, 60, 60);
    [self.view addSubview:sun];
    //2.在圓周上添加地球
    UIImageView *earth = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"a"]];
    earth.center = CGPointMake(CENTER_X+RADIOUS, CENTER_Y);
    earth.bounds = CGRectMake(0, 0, 60, 60);
    [self.view addSubview:earth];
    //3.開啟分線程
    [NSThread detachNewThreadSelector:@selector(newThread:) toTarget:self withObject:earth];
}
//分線程
//處理耗時數(shù)據(jù)
- (void)newThread:(UIImageView *)earth
{
 //無限循環(huán)
    for (; ; )
    {
        [NSThread sleepForTimeInterval:0.01];
        //根據(jù)角度鼓择,計算x三幻,y坐標
        static float angle = 0;
        angle += 1;
        float huDu = angle / 180 * M_PI;
        _x = CENTER_X + RADIOUS * cos(huDu);
        _y = CENTER_Y + RADIOUS * sin(huDu);
        [self performSelectorOnMainThread:@selector(refreshUI:) withObject:earth waitUntilDone:NO];
    }
}
//主線程
//刷新UI
- (void)refreshUI:(UIImageView *)earth
{
    earth.center = CGPointMake(_x, _y);
}

二、UINavigationController
項目:UINavigationController0315
理論:
棧:后進先出 隊列:先進先出

導航(NavController):繼承于UIViewController呐能,根據(jù)棧的原理念搬,實現(xiàn)多個視圖的之間的切換功能抑堡。

界面跳轉(zhuǎn)方法共4種:①root②模態(tài)彈出③
④UINavigationController:
步驟:
1.創(chuàng)建ViewController
2.創(chuàng)建NavigationController,且設置vc為nav的root
3.將nav設置為window的root
進棧:pushViewController: animated:
出棧:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController

源碼:
文件:AppDelegate.m

    //1.創(chuàng)建視圖控制器
    ViewController1 *vc1 = [[ViewController1 alloc]init];
    //2.創(chuàng)建導航控制器,設置vc1為導航的根視圖控制器
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc1];
    //3.將nav作為window的根視圖控制器
    self.window.rootViewController = nav;

文件:ViewController1.m

//下一頁
//從vc1跳轉(zhuǎn)到vc2朗徊,實現(xiàn)壓棧(push/視圖換入)
- (IBAction)nextClick:(UIButton *)sender
{
    //1.創(chuàng)建vc2
    ViewController2 *vc2 = [[ViewController2 alloc]init];
    //2.壓棧首妖,push
    [self.navigationController pushViewController:vc2 animated:YES];
}

文件:ViewController2.m

//從vc2跳轉(zhuǎn)到vc1,實現(xiàn)視圖的換出(pop/出棧)
//由3種方式
- (IBAction)backClick:(UIButton *)sender
{
    //pop
    //方法1:直接返回到上一級界面
    [self.navigationController popViewControllerAnimated:YES];
    //方法2:返回到根視圖控制器
    [self.navigationController popToRootViewControllerAnimated:YES];
    //方法3:
    [self.navigationController popToViewController: animated:YES];
}
//跳轉(zhuǎn)到vc3
- (IBAction)nextClick:(UIButton *)sender
{
    ViewController3 *vc3 = [[ViewController3 alloc]init];
    [self.navigationController pushViewController:vc3 animated:YES];
}

文件:ViewController4.m

//vc4到vc2
- (IBAction)backClick:(UIButton *)sender
{
    //第三種pop
    //先找到vc2
    //1.獲取數(shù)組
    NSArray *vcArr = [self.navigationController viewControllers];
    //2.獲取vc2
    ViewController2 *vc2 = [vcArr objectAtIndex:1];
    //3.popTo vc2
    [self.navigationController popToViewController:vc2 animated:YES];
}

三爷恳、Navigation_Title_Button
項目:Navigation_Title_Button0315
理論:
1.顯示導航欄標題
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title

2.添加導航按鈕
2.1 系統(tǒng)提供的方式
1>initWithBarButtonSystemItem: target: action:
2>initWithTitle: style: target: action:
3>initWithImage: style: target: action:
2.2 自定義方式
步驟:
1.創(chuàng)建button有缆,設置其bounds/action等
2.聲明UIBarButtonItem,并initWithCustomView:button
3.將button添加到nav

源碼:

    //1.顯示導航標題
    //方法1:
    //title:Localized title for use by a parent controller
    self.title = @"主題";
    //方法2:
    //navigationItem:
    //The navigation item used to represent the view controller in a parent’??s navigation bar. (read-only)
    self.navigationItem.title = @"設置";
    
    //2.添加導航按鈕
    //2.1 系統(tǒng)提供的幾種方式
    //(1)系統(tǒng)圖形按鈕
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(photoClick:)];
    //self.navigationItem.rightBarButtonItem = item1;
    //(2)文字按鈕
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
//    self.navigationItem.leftBarButtonItem = item2;
    //(3)圖片按鈕
    //創(chuàng)建圖片
    UIImage *image =  [UIImage imageNamed:@"scratch"];
    //設置圖片渲染模式
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //使用渲染過后的圖片
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(animationButton)];
    //創(chuàng)建item數(shù)組
    NSArray *itemArr = [[NSArray alloc]initWithObjects:item1,item3, nil];
    //將item數(shù)組賦給nav
    self.navigationItem.rightBarButtonItems = itemArr;
    
    //2.2 自定義
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //注意:位置坐標不起作用
//    button.center = CGPointMake(100, 30);
    button.bounds = CGRectMake(0, 0, 40, 40);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithCustomView:button];
    NSArray *itemArr2 = [[NSArray alloc]initWithObjects:item2,item4, nil];
    self.navigationItem.leftBarButtonItems = itemArr2;

四捶障、隱藏導航欄
項目:NavigationBar0315
理論:
設置導航隱藏:self.navigationController.navigationBarHidden = YES;
位置:一般設置在viewWillAppear:

源碼:

//在將要顯示時鸠珠,設置Hidden
//而不是在viewDidLoad設置
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

五掠河、使用storyBoard創(chuàng)建導航欄
項目:Navigation_UseStoryBoard0315
理論:
1.選中storyboard
2.點擊Editor→Embed In→Navigation Controller

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市袖外,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌魂务,老刑警劉巖曼验,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異头镊,居然都是意外死亡蚣驼,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門相艇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來颖杏,“玉大人,你說我怎么就攤上這事坛芽×舸ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵咙轩,是天一觀的道長获讳。 經(jīng)常有香客問我,道長活喊,這世上最難降的妖魔是什么丐膝? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮钾菊,結(jié)果婚禮上帅矗,老公的妹妹穿的比我還像新娘。我一直安慰自己煞烫,他們只是感情好浑此,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著滞详,像睡著了一般凛俱。 火紅的嫁衣襯著肌膚如雪紊馏。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天蒲犬,我揣著相機與錄音朱监,去河邊找鬼。 笑死暖哨,一個胖子當著我的面吹牛赌朋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播篇裁,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼沛慢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了达布?” 一聲冷哼從身側(cè)響起团甲,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎黍聂,沒想到半個月后躺苦,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡产还,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年匹厘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脐区。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡愈诚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出牛隅,到底是詐尸還是另有隱情炕柔,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布媒佣,位于F島的核電站匕累,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏默伍。R本人自食惡果不足惜欢嘿,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望也糊。 院中可真熱鬧炼蹦,春花似錦、人聲如沸显设。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捕捂。三九已至瑟枫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間指攒,已是汗流浹背慷妙。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留允悦,地道東北人膝擂。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像隙弛,于是被迫代替她去往敵國和親架馋。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多全闷,會對里面所有的內(nèi)容的引用計數(shù)+1叉寂,想要解決就用__block...
    炙冰閱讀 2,492評論 1 14
  • 1.簡介 UINavigationController用來管理視圖控制器,在多視圖控制器中常用总珠。它以棧的形式管理視...
    滴兜滴兜閱讀 5,616評論 0 3
  • 1,Search Bar 怎樣去掉背景的顏色(storyboard里只能設置background顏色屏鳍,可是發(fā)現(xiàn)cl...
    以德扶人閱讀 2,373評論 2 50
  • 晨起霧朦朦,拂面清風局服。憑欄遠眺幾重重钓瞭。滴露含羞說清夢,稚草偷聽淫奔。 天曉鴻雁鳴山涡,書信一封,他鄉(xiāng)故友寄春情搏讶。從來只多嘆...
    北名文學閱讀 235評論 0 1
  • 作業(yè):讀書是為了明理媒惕,明理是為了合于道并且成為自己處世的習慣系吩。那么,讀書五年妒蔚,思考五年穿挨,然后可以五十年不思考也不會...
    向陽開延麗閱讀 218評論 0 0