从FirstViewController跳转到SecondViewController,当从SecondViewController返回时,如果想把数据回传给FirstViewController,可以用代理的方法,FirstViewController中这样使用:
FirstViewController.h
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>
FirstViewController.m
- (void)pushNextViewControl:(UIBarButtonItem *)button{
SecondViewController * showVC = [[SecondViewController alloc]init];
showVC.text = _textField.text;
// 将代理对象设置成SecondViewController
showVC.delegate = self;
[self.navigationController pushViewController:showVC animated:YES];
[showVC release];
}
// FirstViewController实现协议里面的方法
- (void)showViewGiveValue:(NSString *)text{
_inputLabel.text = text;
}
SecondViewController需要这样设置代理:
SecondViewController.h
@protocol SecondViewControllerDelegate <NSObject>
@optional
- (void)showViewGiveValue:(NSString *)text;
@end
@interface SecondViewController : UIViewController
@property (nonatomic,copy)NSString * text;
// 定义一个代理
@property (nonatomic,assign)id<SecondViewControllerDelegate> delegate;
@end
SecondViewController.m
- (void)popPerView:(UIBarButtonItem *)barButton{
// 在页面跳转前将参数传出去
if ([self.delegate respondsToSelector:@selector(showViewGiveValue:)]) {
[self.delegate showViewGiveValue:_showTextField.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
本文介绍如何在iOS应用的两个视图控制器之间通过代理模式进行数据传递。具体展示了FirstViewController如何设置自身为SecondViewController的代理,并在返回时接收数据。
561

被折叠的 条评论
为什么被折叠?



