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
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zMwgjN1cDN0EDOycDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
在頁面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);
}