天天看點

iOS傳值一屬性傳值,代理傳值

1.屬性傳值

屬相傳值一般從前向後,即從A到B,要傳什麼類型值,就在B頁面定義什麼樣的屬性

如要将一個字元串從A傳到B,就在B頁面設定以下屬相

@property(nonatomic,retain) NSString *str ;//接受的值

在A控制器跳轉方法内實作

- (void)showSecondView:(UIButton*)btn

{

SecondViewController *VC = [[SecondViewController alloc] init

];

VC.str =@"我是屬性傳值"

;

[self presentViewController:VC animated:YES completion:nil];

}

屬性傳值,比較簡單,也非常常用

2.delegate

iOS傳值一屬性傳值,代理傳值

 在頁面B設定協定及方法          

view source print ? //SecondViewController.h view source print ?

//設定代理

@protocol

 s

econdViewDelegate

//代理方法

(為防止循環引用用weak修飾)

-(

void

)showMessage:(NSString *)messageStr;

@end

view source print ?.

view source print

//SecondViewController.h

view source print ?

@interface

SecondViewController : UIViewController

@property

(nonatomic, weak)id<secondViewDelegate> delegate;

@end

   調用 view source print ?

//SecondViewController.m

- (void)backFirst:(UIButton*)btn{

[self.delegate showMessage:@"我是代理傳值"];

[self dismissViewControllerAnimated:YES completion:nil];

}

在頁面A顯示   view source print ?//FirstViewController.m

-(

void

)showName:(NSString *)messageStr{

NSLong(@"%@",messageStr);

}