學習文章http://www.lxweimin.com/p/533d0c3caeba
常用的在頁面中傳值有兩個方法:
一個是委托代理
另一個是利用Block
簡述
實現兩個ViewController.
一個是FirstViewController, 另一個是SecondViewController.
主要是在SecondViewController中的文本框輸入文字之后
將值傳入到FirstViewController的Lable中進行顯示.
其中UI設置部分的代碼省略
實現步驟
在SecondViewController中設置一個屬性塊, 其中參數是NSString類型, 用于接收要傳的參數.
typedef void (^SYBlockText)(NSString *value);
@interface SecondViewController : UIViewController
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) UIButton *button;
@property (copy, nonatomic) SYBlockText myBlock;
@end
然后在FirstViewController中對視圖中返回到SecondViewController的Button設置動作函數
- (void)toSencondViewController {
SecondViewController *SecondVC = [[SecondViewController alloc] init];
__weak FirstViewController *weakSelf = self; //弱引用轉換,為了防止循環引用
SecondVC.myBlock = ^(NSString *value) {
weakSelf.lable.text = value;
};
[self presentViewController:SecondVC animated:YES completion:nil];
}
之后再SecondViewController中對視圖中返回到FirstViewController的Button設置動作函數
- (void)returnFirstViewController{
self.myBlock(self.textField.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
效果圖
QQ20160907-0@2x.jpg