所以,我意识到这与其他几十个问题非常相似,所以让我向你们保证,我已经读过它们,了解MVC,读过书等等......但我仍然难过。
我有两个班级...... A和B. A称B为模态瞄准。 B有一个“取消”按钮,它会自动解除并返回A.我希望在发生这种情况时调用A上的方法(或设置属性)。
我已将A导入B并制作了它的iVar(也试过了一个属性),在ViewDidLoad中分配它,然后尝试各种方法设置属性或从方法调用A上的方法(IBAction或prepareForSegue )B。
属性似乎没有效果......没有任何明显变化,并且它们记录为未更改。 我可以获取我的方法的NSLog - 这是一个令人鼓舞的健全性检查,但方法中的其他代码都没有触发......
这是我目前的版本:
A类:
有一个隐藏在VDL上的ImageView。 我要么试图让它被取消隐藏 - 要么失败,要么在我的方法被调用之前永远不要创建它....
- (void) makeGoalVisible {
NSLog(@"GOAL"); // this prints
UIImageView *goal = [[UIImageView alloc] initWithFrame:CGRectMake(906, 442, 61, 93)];
goal.image = [UIImage imageNamed:@"goalIndicator@2x.png"];
[self.view addSubview:goal]; // this method works from WITHIN Class A - so I know the code/position is fine
}
B组:
- (IBAction)pressedCancel:(id)sender {
NSLog(@"CANCEL");
[self performSegueWithIdentifier:@"setGoal" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"PREP");
TimelineViewController *host = [[TimelineViewController alloc]init];
[host makeGoalVisible]; // this calls the method but the method doesnt work
//[host.goalIndicator setHidden:NO]; // this doesnt work
}
方法中的所有日志按预期顺序打印...就像我说的那样,该方法是从B类调用的,如果我从A类调用它而不是B类,则调用该方法中的代码...
我缺少什么?
答案 0 :(得分:3)
在使用UIViewController
调用此segue
时尝试此代码,以便您可以使用[segue destinationViewController];
获取对象,因为无需使用TimelineViewController *host = [[TimelineViewController alloc]init];
<创建新的obejct / p>
所以这就是你的代码打印NSLog(@"GOAL");
但是从单独的对象
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"PREP");
TimelineViewController *host = [segue destinationViewController];
[host makeGoalVisible];
//[host.goalIndicator setHidden:NO];
}
答案 1 :(得分:1)
你的基本前提是错误的。如果你使用A的模态推送,你需要调用dismissViewControllerAnimated:completion:explicit,或者使用unwind segue。
如果您使用performSegueWithIdentifier:
从B到A,那么您正在创建A的新副本,而不是返回到A的原始实例。您正在创建一个不断增加的模态视图控制器堆栈,这是错误的。
从B回到A的正常通信技术是将A设置为A的prepareForSegue中的B代表。然后,在B中,您使用委托在A中进行呼叫/设置属性。