某些代码未在从另一个ViewController调用的方法中执行 [英] Some code not executing in a method called from another ViewController
问题描述
我的问题
我有一个标准的UIViewController
.按下按钮,它将加载表单模态视图控制器.按下UIBarButtonItem
键消除此模式视图时,我通过执行以下操作来调用方法:
My problem
I have a standard UIViewController
. With the press of a button, it loads a form sheet modal view controller. When dismissing this modal view with the press of a UIBarButtonItem
I call a method by doing:
ViewController *main = [[ViewController alloc] initWithNibName:nil bundle:nil];
[main updateLabel];
在主ViewController
中的方法-(void)updateLabel
中,我设置了标签文本,但标签不会更改.但是我知道该函数会被调用,因为如果我执行NSLog(@"Method call test);
而不是label.text = @"Test"
,则可以在控制台中看到该消息.
In the method -(void)updateLabel
in the main ViewController
I'm setting the text of a label, but the label won't change. But I know the function gets called, because if I do a NSLog(@"Method call test);
instead of label.text = @"Test"
I can see the message in console.
我做错了什么?这一定是我在主ViewController
中调用方法的方式,因为我可以轻松地在其他任何地方更改标签.
What am I doing wrong? It must be the way I'm calling the method in the main ViewController
, because I can easily change the label anywhere else.
我要做什么:
在关闭模式视图控制器时,我希望在主视图控制器中调用一个方法,在这种情况下,请更改标签的文本.
What I want to do:
When dismissing a modal view controller, I want a method to be called in the main view controller, and in this case change the text of a label.
感谢您的帮助!
推荐答案
您正在使用该代码创建ViewController的新实例,而没有获得指向您现有实例的指针.
You're creating a new instance of ViewController with that code, not getting a pointer to the one you already have.
如果ViewController是呈现模态视图的控制器,那么您可以使用指向它的指针,
If ViewController is the controller that presented the modal view, then you can get a pointer to it with,
ViewController *main = self.presentingViewController;
这篇关于某些代码未在从另一个ViewController调用的方法中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!