因此,我意识到这与其他许多问题非常相似,所以让我向您保证我已经阅读过它们、理解 MVC、阅读过书籍等等……但我仍然感到困惑。
我有两个类...A 和 B。A 将 B 称为模态转场。 B 有一个“取消”按钮,它会自动关闭并返回到 A。发生这种情况时,我想在 A 上调用一个方法(或设置一个属性)。
我已经将 A 导入 B 并制作了它的 iVar(也尝试了一个属性),在 ViewDidLoad 中分配了它,然后我尝试了各种方法来设置属性或从方法(IBAction 或 prepareForSegue)调用 A 上的方法) 的 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 类调用它,则调用方法中的代码...
我错过了什么???
最佳答案
尝试使用 segue
调用此 UIViewController
时使用此代码,这样您就可以使用 [segue destinationViewController];
获取对象,因为没有需要使用 TimelineViewController *host = [[TimelineViewController alloc]init];
所以这就是你的代码打印 NSLog(@"GOAL");
但是从单独的对象调用的原因
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"PREP");
TimelineViewController *host = [segue destinationViewController];
[host makeGoalVisible];
//[host.goalIndicator setHidden:NO];
}
关于ios - 来自另一个类的 Cocoa Touch 方法被调用但代码不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759401/