界面傳值的四種方式

界面傳值app

四種傳值的方式atom

一、屬性傳值(從前日後)線程

步驟:代理

 一、屬性傳值用於第一個界面向第二個界面傳值orm

 二、明確兩者聯繫的橋樑,也就是觸發跳轉的地方對象

 三、明確傳輸的值,類型是什麼繼承

 四、在第二個視圖控制器內部聲明相對應類型的屬性,來接受傳輸的值string

 五、在第二個界面使用傳入的值it

 

二、代理傳值(從後往前)io

步驟:

一、聲明協議

UI中的協議名稱 爲 當前類名 + Delegate

 

@protocol FourViewControllerDelegate<NSObject>

- (void)pushValue:(NSString *)text;

- (void)pushValue1:(UIColor *)color;

@end

 

二、聲明代理

@property (nonatomic, assign) id<FourViewControllerDelegate>delegate;

 

三、讓代理執行方法(在頁面跳轉處)

//判斷代理人不爲空,而且代理人響應了協議中的方法

 if (self.delegate != nil && [self.delegate respondsToSelector:@selector(pushValue:)]) {

 

        [self.delegate pushValue:self.textField.text];

        [self.delegate pushValue1:self.view.backgroundColor];

 

    }

 

四、指定代理人爲自身

- (void)button {

    FourViewController *four = [[FourViewController alloc] init];

    //四、指定代理對象爲本身

    four.delegate = self;

    [self.navigationController pushViewController:four animated:YES];

   

    

}

 

五、接受協議

@interface ThirdViewController : UIViewController<FourViewControllerDelegate>

六、實現協議方法

- (void)pushValue:(NSString *)text {

    _lable.text  = text;

}

 

- (void)pushValue1:(UIColor *)color{

    self.view.backgroundColor = color;

}

 

三、block傳值(從後往前)

有兩種方式:

1、使用block屬性實現回調傳值

步驟:

一、定義須要傳的值得類型

typedef void (^Bada)(NSString *);

typedef void  (^Color) (UIColor *);

 

二、將Bloak聲明成屬性必須用copy修飾

@property (nonatomic, copy) Bada bada;

@property (nonatomic, copy) Color color;

 

三、

- (void)button {

    //執行Block

    if (self.bada != nil) {

    self.bada(self.textField.text);

    }

    

    if (self.color != nil) {

        self.color (self.view.backgroundColor);

    }

 

    [self.navigationController popToRootViewControllerAnimated:YES];

    

}

四、對block進行實現

SecondViewController *second = [[SecondViewController alloc] init];

    

    second.bada = ^(NSString *str) {

        self.lable.text = str;

    };

    second.color = ^(UIColor *color) {

        self.view.backgroundColor = color;

    };

    [self.navigationController pushViewController:second animated:YES];

方式二:在方法中定義block實現回調傳值

步驟:

一、在First.h文件中,重定義void(^)(NSString *string)類型 的別名爲FirstBlock
typedef void(^FirstBlock)(NSString *string); 

二、聲明方法,在方法中封裝block 

-(void)sendNumber:(NSInteger )number andBlock: (FirstBlock)block; 

 

三、在First.m實現 法,並執 block

 -(void)sendNumber:(NSInteger )number andBlock: (FirstlBlock)block;

    NSString *string = [NSString

stringWithFormat:@"%ld",number];

    block(string);

}

四、 執行放法,實現block並接收回傳過來的string值 

First*first= [First alloc] init];

 //執行方法

[first sendNumber:12243432 andBlock:^(NSString 

*string) {

        self.label.text = string;

}]; 

 

 

 

 

四、單例傳值(可用於從第一頁面直接傳值給第十個頁面,既能夠從前日後,也能夠從後往前)

步驟:

一、建立一個類繼承自NSObject,在.h文件中聲明一個類方法。

//單利方法是類方法, 返回值類型爲instancetype

//本身定義的單例類,方法名一般都是以share開頭

+ (instancetype)shareInstance;

二、在.m文件中寫類方法的實現

static Handler *handler = nil;

 

@implementation Handler

 

//怎麼保證 建立的 當劉對象是惟一的?

+ (instancetype)shareInstance {

    

    @synchronized(self) {//加鎖,保證建立過程同一時間內只容許一個線程對象訪問,保證其惟一性(self 任意對象)

        

    if (handler == nil) {

       handler = [[Handler alloc] init];

    }

        

    }

    return handler;

    

}

三、藉助視圖的即將出現和即將消失的方法,能夠實現值得傳遞。(注意視圖的加載和建立方法只會走一次,可是視圖的將出現和即將消失的方法會走屢次)

- (void)viewWillDisappear:(BOOL)animated {

    //建立單例

    Handler *handler = [Handler shareInstance];

    handler.string= self.textField.text;

}

- (void)viewWillAppear:(BOOL)animated {

    Handler *handler = [Handler shareInstance];

    self.textField.text = handler.string;

}

相關文章
相關標籤/搜索