我有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中的答案 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];
}