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


 在頁面B設置協議及方法          

//SecondViewController.h
//設置代理@protocol secondViewDelegate//代理方法(爲防止循環引用用weak修飾)
-(void)showMessage:(NSString *)messageStr;
@end
//SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, weak)id<secondViewDelegate> delegate;
@end
   調用
//SecondViewController.m
- (void)backFirst:(UIButton*)btn{
[self.delegate showMessage:@"我是代理傳值"];
[self dismissViewControllerAnimated:YES completion:nil];

}
在頁面A顯示  
-(void)showName:(NSString *)messageStr{
NSLong(@"%@",messageStr);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章