dismissViewControllerAnimated没有将数据传回vc1

时间:2014-06-07 12:18:50

标签: ios objective-c

我有vc1和vc2。当vc2被解除时,我需要vc2来设置vc1的变量。

这是vc1.h

中的相关代码
- (IBAction)btnclick:(id)sender;
@property (strong, nonatomic) NSString *mystr;

这是vc1.m

中的相关代码
- (IBAction)btnclick:(id)sender {
    UIStoryboard * storyboard = self.storyboard;
    NSString * storyboardName = [storyboard valueForKey:@"name"];
    //In storyboard I named vc2 "ctrl2" as identifier 
    ViewController2 *temp = 
    [[UIStoryboard storyboardWithName:storyboardName bundle:nil]     instantiateViewControllerWithIdentifier:@"ctrl2"];
    [self presentViewController:temp animated:YES completion:NULL];
}

- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"In viewWillAppear mystr is %@", self.mystr);
}

- (void)viewDidLoad{
    [super viewDidLoad];
    NSLog(@"my str is %@", self.mystr);
}

这是vc2.h中的相关代码

@class ViewController;

- (IBAction)btnclkd:(id)sender;
@property(nonatomic, weak)ViewController *vc1Obj;

这是vc2.m

中的相关代码
- (IBAction)btnclkd:(id)sender {
    self.vc1Obj.mystr = @"test";
    NSLog(@"setting mystr from v2");
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
vc1中的
viewDidLoad在vc2被解除时不会被触发。所以我试图在viewDidAppear和viewWillAppear中记录它们。 在这两种情况下,mystr始终为null。我做错了什么?

1 个答案:

答案 0 :(得分:0)

在我看来,好像你永远不会设置vc2.vc1Obj

我的建议是在vc1 - (IBAction)btnclick:(id)sender;中执行此操作:

- (IBAction)btnclick:(id)sender 
{
    //    in storyboard I named vc2 "ctrl2" as identifier 
    ViewController2 *temp = [self.storyboard instantiateViewControllerWithIdentifier:@"ctrl2"];
    temp.vc1Obj = self;
    [self presentViewController:temp animated:YES completion:NULL];
}