18HWThread_UINavigationController_Title_Button_Hidden_UseStoryBoard

大綱

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

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

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

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

三、Navigation_Title_Button
項目:Navigation_Title_Button0315
理論:
1.顯示導(dǎo)航欄標(biāo)題
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title

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

四、隱藏導(dǎo)航欄
項目:NavigationBar0315
理論:
設(shè)置導(dǎo)航隱藏:self.navigationController.navigationBarHidden = YES;
位置:一般設(shè)置在viewWillAppear:

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

正文

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

- (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坐標(biāo)
        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
理論:
棧:后進先出 隊列:先進先出

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

界面跳轉(zhuǎn)方法共4種:①root②模態(tài)彈出③
④UINavigationController:
步驟:
1.創(chuàng)建ViewController
2.創(chuàng)建NavigationController,且設(shè)置vc為nav的root
3.將nav設(shè)置為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)建導(dǎo)航控制器,設(shè)置vc1為導(dǎo)航的根視圖控制器
    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.顯示導(dǎo)航欄標(biāo)題
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title

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

源碼:

    //1.顯示導(dǎo)航標(biāo)題
    //方法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 = @"設(shè)置";
    
    //2.添加導(dǎo)航按鈕
    //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"];
    //設(shè)置圖片渲染模式
    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];
    //注意:位置坐標(biāo)不起作用
//    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;

四、隱藏導(dǎo)航欄
項目:NavigationBar0315
理論:
設(shè)置導(dǎo)航隱藏:self.navigationController.navigationBarHidden = YES;
位置:一般設(shè)置在viewWillAppear:

源碼:

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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