所以我能够将数据从第一个视图传输到第二个视图,如下所示:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Check Mark Segue"])
{
NSLog(@"Transfering Data");
AutoRenewDrop *controller = segue.destinationViewController;
controller.transferData = self.renewDate.text;
}
}
但是,当用户点击完成并且transferData正常工作但updateDate.text没有更改时,我尝试将新值传回updateDate.text。以下是我用来传输数据的代码:
-(IBAction)done:(UIStoryboardSegue *)segue {
[self.navigationController popViewControllerAnimated:YES];
AddR *add = [[AddR alloc] init];
add.renewDate.text = transferData;
}
有人可以告诉我如何解决这个问题吗?
答案 0 :(得分:3)
您需要将包含第一个视图引用的属性添加到第二个视图中:
@interface AutoRenewDrop
@property(weak, nonatomic) AddR *callerView;
@end
然后在第二个视图的done方法中,您只需更新调用者视图中的变量:
-(IBAction)done:(UIStoryboardSegue *)segue {
[self.navigationController popViewControllerAnimated:YES];
callerView.renewDate.text = transferData;
}
当然,当您实例化第二个视图时,您必须以这种方式设置引用:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Check Mark Segue"])
{
NSLog(@"Transfering Data");
AutoRenewDrop *controller = segue.destinationViewController;
controller.transferData = self.renewDate.text;
controller.callerView = self; //Here, you are passing the reference to this View
}
}
答案 1 :(得分:0)
您应该使用委托。 您可以阅读有关协议和委托here的非常好的教程。
假设您有2个ViewControllers - VC1和VC2,并且假设VC1显示VC2。
在VC2.h中
@protocol VC2Delegate <NSObject>
- (void)updateData:(id)theData;
@end
@interface
@property (nonatomic, weak) id<VC2Delegate> delegate;
@end
在VC2.m
中@syntesize delegate;
- (void)done
{
[delegate updateData:myData];
}
在VC1.h
@interface VC1 : UIViewController<VC2Delegate>
在VC1.m
中- (void)goToVC2
{
VC2 *vc2 = [[VC2 alloc] init];
vc2.delegate = self;
// present vc2
}
- (void)updateData:(id)data
{
// update what you need
}