關(guān)系A(chǔ) push B
界面A向界面B傳值用屬性傳值, B接收可重寫(xiě)setter方法
界面B傳值給A, 也就是回傳, 用代理
第一步: 在B.h, B簽協(xié)議并聲明方法
@protocol ViewControllerDelegate <NSObject>
- (void)passLocation:(NSString *)locationText;
@end
第二步, 在B.h, 聲明屬性, 注意是weak
@property (nonatomic, assign) id<ViewControllerDelegate>delegate;
第三步: 在B.m, 在適當(dāng)?shù)臅r(shí)機(jī)傳值
//判斷代理中的方法是否被實(shí)現(xiàn),避免未被實(shí)現(xiàn)代理的程序崩潰
if ([self.delegate respondsToSelector:@selector(passLocation:)]) {
[self.delegate passLocation:[NSString stringWithFormat:@"緯度:%f, 經(jīng)度%f", view.annotation.coordinate.latitude, view.annotation.coordinate.longitude]];
}
第四步, 來(lái)到A.m, 簽協(xié)議,
@interface FirstViewController ()<ViewControllerDelegate>
第五步: 在A.m, 找個(gè)合適的地方, B指定代理人為A
self.vc.delegate = self;
第六步: 在A.m, 找個(gè)空白地方實(shí)現(xiàn)協(xié)議方法
- (void)passLocation:(NSString *)locationText{
_label.text = locationText;
}