天天看點

iOS基本控制-UINavigationController 傳統的價值觀,代理傳統價值觀,正向傳統價值觀,反傳統的價值觀

/*

 程式過程:

 1。建立一個根視圖,一個二級視圖

 2,根視圖NavigationItem.title = Root 二級視圖NavigationItem.title

= Second

 根視圖NavigationItem.rightButton入棧二級視圖

 3,

二級視圖中建立三個buttonbutton一 button二 button三

三個button點選時間都是出棧。并把自己的button的

 titel 賦給根視圖的NavigationItem.title

 4。當再次進入二級視圖時,推斷根視圖的NavigationItem.title和哪個button的title一樣。假設

 一樣。就把button的title顔色設定為紅色。

                **總結

     1,從第一層向第二層正向傳參時:

          在第二層頁面中建立一個接收這個參數的屬性

          在第一層向第二層跳轉時 建立完第二層的執行個體對象,

 */

<SendValue.h>

#import

<Foundation/Foundation.h>

@protocol SendValue <NSObject]]>

- (void)sendBtnTitle:(NSString*)title;

@end

<XSAppDelegate.m>

"XSAppDelegate.h"

#import "XSRootViewController.h"

@implementation XSAppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{

    self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor=

[UIColorwhiteColor];

    XSRootViewController*rootViewController = [[XSRootViewControlleralloc]init];

    UINavigationController*navController = [[UINavigationControlleralloc]initWithRootViewController:rootViewController];

    self.window.rootViewController=

navController;

    [self.windowmakeKeyAndVisible];

    return

YES;

}

<XSRootViewController.m>

"XSRootViewController.h"

#import "XSSecondViewController.h"

@interface XSRootViewController()

@implementationXSRootViewController

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

    self

= [super

initWithNibName:nibNameOrNil

bundle:nibBundleOrNil];

    if

(self) {

        // Custom initialization

    }

self;

- (void)viewDidLoad

    [super

viewDidLoad];

// Do any additional setup after loading the view.

    self.view.backgroundColor=

[UIColoryellowColor];

    self.navigationItem.title=

@"Root";

    UIBarButtonItem*btnItem = [[UIBarButtonItemalloc]initWithTitle:@"Push"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick:)];

    self.navigationItem.rightBarButtonItem=

btnItem;

//#pragma  mark --SendVaule

- (void)sendBtnTitle:(NSString*)title

title;

- (void)btnClick:(UIBarButtonItem*)btnItem

    XSSecondViewController*secondViewController = [[XSSecondViewControlleralloc]init];

    secondViewController.delegate=

    secondViewController.currentTitle=

self.navigationItem.title;

    [self.navigationControllerpushViewController:secondViewControlleranimated:YES];

<XSSecondViewController.h>

<UIKit/UIKit.h>

#import "SendValue.h"

@interface XSSecondViewController : UIViewController

//定義代理

@property (nonatomic,assign)id<SendValue>

delegate;

//建立一個正向傳值的屬性,

@property (nonatomic,copy)NSString*currentTitle;

<XSSecondViewController.m>

"XSSecondViewController.h"

@interface XSSecondViewController()

@implementationXSSecondViewController

    UIBarButtonItem*btnItem = [[UIBarButtonItemalloc]initWithTitle:@"Pop"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick)];

    self.navigationItem.leftBarButtonItem=

[UIColorblueColor];

    UIButton*btn1 = [UIButtonbuttonWithType:UIButtonTypeSystem];

    btn1.frame=

CGRectMake(10, 80, 300, 40);

    [btn1 setTitle:@"按鍵一"forState:UIControlStateNormal];

    [btn1 setBackgroundColor:[UIColorwhiteColor]];

    [btn1 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    btn1.tag= 1;

    //假設button的标題和屬性中的_currentTitle同樣,即和根頁面中的導覽列的title一樣

([_currentTitleisEqualToString:btn1.currentTitle])

        btn1.selected=

    //假設selected為YES就運作setTitleColor

    [btn1 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];

    [self.viewaddSubview:btn1];

    UIButton*btn2 = [UIButtonbuttonWithType:UIButtonTypeSystem];

    btn2.frame=

CGRectMake(10, 130, 300, 40);

    [btn2 setTitle:@"按鍵二"forState:UIControlStateNormal];

    [btn2 setBackgroundColor:[UIColorwhiteColor]];

    [btn2 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    btn2.tag= 2;

([_currentTitleisEqualToString:btn2.currentTitle])

        btn2.selected=

    [btn2 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];

    [self.viewaddSubview:btn2];

    UIButton*btn3 = [UIButtonbuttonWithType:UIButtonTypeSystem];

    btn3.frame=

CGRectMake(10, 180, 300, 40);

    [btn3 setTitle:@"按鍵三"forState:UIControlStateNormal];

    [btn3 setBackgroundColor:[UIColorwhiteColor]];

    [btn3 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    btn3.tag= 3;

([_currentTitleisEqualToString:btn3.currentTitle])

        btn3.selected=

    [btn3 setTitleColor:[UIColorredColor]forState:UIControlStateSelected];

    [self.viewaddSubview:btn3];

- (void)btnClick

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

- (void)btnClick:(UIButton*)btn

    //取出button的标題

    NSString*title =  btn.currentTitle;

    //推斷代理中是否有sendBtnTitle:這個函數

([_delegate

respondsToSelector:@selector(sendBtnTitle:)]) {

        //代理運作自己的sendBtnTitle函數,傳參是title

        [_delegatesendBtnTitle:title];

版權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。