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

查看:158
本文介绍了另一个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];

}


$ b < , 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实例这个类的新实例)。此行:

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的模式。有很多web和SO资源如何做到这一点,所以我不会在这里重复。 查看这个,例如或googleiOS委托。

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天全站免登陆