1

所以我能够将数据从第一个视图传输到第二个视图,如下所示:

- (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 工作正常但 renewDate.text 没有改变时,我尝试将新值传输回 renewDate.text 。这是我用来传回数据的代码:

-(IBAction)done:(UIStoryboardSegue *)segue {

[self.navigationController popViewControllerAnimated:YES];
AddR *add = [[AddR alloc] init];
add.renewDate.text = transferData;

}

有人可以告诉我如何解决这个问题吗?

4

2 回答 2

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
      }
}
于 2012-11-03T14:18:59.707 回答
0

您应该使用委托。您可以在此处阅读有关协议和委托的非常好的教程。

假设您有 2 个 ViewController - 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
}
于 2012-11-03T14:23:22.847 回答