【发布时间】:2014-08-10 08:04:48
【问题描述】:
我有两个类,想在按下按钮时从一个类调用一个方法。我在我的 .h 文件中这样声明它:
-(void) imageChange;
我在我的 .m 中创建了这样的方法:
-(void)imageChange {
UIImage *image = [UIImage imageNamed: img];
[_MyImage setImage:image];
}
最后,我尝试使用以下方法从另一个类调用该方法:
- (IBAction)Done:(id)sender {
SecondViewController *theInstance = [[SecondViewController alloc] init];
[theInstance imageChange];
[self dismissViewControllerAnimated:YES completion:nil];
}
但是,当我在视图控制器中按“完成”时,UIImage 不会改变。请注意:img 是一个 NSString 值。
【问题讨论】:
-
您正在创建一个新的
SecondViewController并更改其图像。那是没有意义的。您需要引用现有的SecondViewController并更改其图像。 -
@rmaddy 我应该更改什么代码?
-
@user3750491,与其说是改变不如说是增加:SecondViewController 是否呈现了这个视图控制器?
-
@danh 你能解释一下吗?我是 Objective-C 的新手。谢谢!
-
使用delegation!
标签: ios objective-c cocoa-touch uiviewcontroller