从另一个 UIViewController 调用方法没有可见效果 [英] Calling Method From Another UIViewController Has No Visible Effect

查看:16
本文介绍了从另一个 UIViewController 调用方法没有可见效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,想在按下按钮时从一个类中调用一个方法.我在我的 .h 文件中这样声明:

I have two classes and want to call a method from one class when a button is pressed. I declare it in my .h file as so:

-(void) imageChange;

我在我的 .m 中创建了这样的方法:

And I created the method in my .m like this:

-(void)imageChange {
    UIImage *image = [UIImage imageNamed: img];
    [_MyImage setImage:image];
}

最后,我尝试使用以下方法从另一个类调用该方法:

Finally, I tried to call the method from another class using:

- (IBAction)Done:(id)sender {

    SecondViewController *theInstance = [[SecondViewController alloc] init];
    [theInstance imageChange];
    [self dismissViewControllerAnimated:YES completion:nil];

}

但是,当我在视图控制器中按完成"时,UIImage 不会改变.请注意:img 是一个 NSString 值.

However, when I press "done" in my view controller, the UIImage doesn't change. Please note: img is an NSString value.

推荐答案

该问题是常见问题的变体:如何在视图控制器之间传递值",您的代码代表了对解决方案的常见尝试.让我们从你做了什么开始.

The question is a variation on a common one: "how to pass values between view controllers", and your code represents a common attempt at a solution. Let's start with what you did.

您的应用程序有两个视图控制器,它们的视图位于视图堆栈上,您希望在它们之间进行通信.这一行:

Your app has two view controllers with views on the view stack, and you want to communicate something between them. This line:

SecondViewController *theInstance = [[SecondViewController alloc] init];

创建一个全新 SecondViewController 实例(alloc 意味着为此类的新实例分配内存).这一行:

creates a brand new instance of SecondViewController (alloc means allocate memory for a new instance of this class). This line:

[theInstance imageChange];

向它传达一些信息,在您的情况下,它看起来像是设置图像视图的图像.然后这一行:

communicates something to it, in your case it looks like setting an image view's image. Then this line:

}

隐式销毁该新实例,因为它再也不会被引用.因此,您的代码成功地与 SecondViewController 通信,但使用了错误的实例,该实例仅存活了几毫秒.

implicitly destroys that new instance, since it's never referred to again. So your code succeeds in communicating with a SecondViewController, but with the wrong instance, an instance that lives for only a few milliseconds.

好的,怎么办?@rmaddy 所说的是找到 SecondViewController 的现有实例,并与之通信.如何获得现有实例取决于我们如何到达这里.你的代码中的dismissViewControllerAnimated 让我认为这个当前的vc 是由SecondVC 的一个实例呈现的.如果是这样,

Okay, what to do about it? What @rmaddy was saying is go find the existing instance of SecondViewController, and communicate with that. How to get ahold of that existing instance depends on how we got here. The dismissViewControllerAnimated in your code makes me think that this current vc was presented by an instance of SecondVC. If so,

(SecondViewController *)self.presentingViewController

指向您需要的内容.如果我们在 UINavigationController 中,你可以挖掘它的 viewControllers 堆栈,可能在这里:

points to what you need. If we were in a UINavigationController, you could dig through it's viewControllers stack, probably here:

NSArray *stack = self.navigationController.viewControllers;
SecondViewController *secondVC = stack[stack.count-2];

但是,虽然这可能是从 A 到 B 的最直线,但这并不是一个很好的设计,因为它使当前的视图控制器以一种脆弱的方式依赖于它的呈现方式.

But, while all that might be the straightest line from A to B, it's not very good design because it makes this current view controller dependent in a brittle way on how it got presented.

因此@CrimsonChris 提出了一个考虑委托的好建议.对于需要与其他人交流的 vc,这是大多数人的首选模式.有很多关于如何做到这一点的网络和 SO 资源,所以我不会在这里重复.看看这个,例如,或谷歌iOS 委托".

Hence @CrimsonChris makes a good suggestion to consider delegation. This is most peoples' go-to pattern for a vc that needs to communicate with another. There are plenty of web and SO resources on how to do this, so I won't repeat here. Check out this, for example, or google "iOS delegation".

还有其他方法,例如 NSNotificationCenter 可以向所有感兴趣的人广播您想要交流的任何内容,或者 KVO 可以让 SecondVC 观察模型中的变化并做出反应,无论如何或为什么进行了更改.

There are other ways, like NSNotificationCenter to broadcast to everyone that's interested whatever you want to communicate, or KVO that lets the the SecondVC observe a change in your model and react, no matter how or why that change was made.

后两者的关键概念是您的应用程序需要有一个模型,一组描述应用程序状态的对象.视图控制器不是模型——事实上它们完全不是模型.他们的工作是了解模型更改并相应地修改视图.

The key concept for these latter two is that your app needs to have a model, a set of objects that describes the app state. The view controllers are not the model -- in fact they're exactly not the model. Their job is to learn about model changes and modify the views accordingly.

对于您的情况,您发布的代码不应该尝试在另一个视图控制器中设置图像,它应该将发生的触发解雇的任何用户操作记录到模型中.当这个当前的 vc 关闭自己时,SecondViewController(假设它做了现在)将获得一个 viewWillAppear.该方法可能是检查在用户操作上设置的模型条件的好地方.然后 SecondViewController 可以调用 imageChange 自身.

For your case, the code you posted shouldn't be trying to get an image set in the other view controller, it should be recording into the model whatever user action that happened that triggers the dismissal. When this current vc dismisses itself, the SecondViewController (assuming it did the present) will get a viewWillAppear. That method might be a good place to check the model condition that was set on the user action. Then SecondViewController can call imageChange on itself.

希望这已经足够清楚了.祝你好运.

Hope that was clear enough. Good luck.

这篇关于从另一个 UIViewController 调用方法没有可见效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆