从另一个类方法更新UI - Cocoa [英] Update UI from another Class Method - Cocoa
问题描述
我想从AppDelegate更新我的应用程序中的UI,但是每当我这样调用它:
I would like to update the UI in my application from the AppDelegate, but whenever I call it as so:
Controller *object = [[Controller alloc] init];
[object methodHere];
似乎没有更新UI。我在这里做错了什么?我把一个NSLog,看看是否被调用,它是。 这里是显示错误的示例项目。
It doesn't seem to update the UI. What am I doing wrong here? I have put in a NSLog to see if it was being called, and it is. Here is a sample project that shows the error.
编辑:有人可以告诉我什么改变到我提供的项目。我只是不知道要输入到我的项目,以便我可以更改一个简单的NSTextField的值从另一个类。
Can someone just show me what to change to the project I provided. I just don't know what to type into my project so that I can change the value of a simple NSTextField from another class.
推荐答案
这很简单,Chuck的意见基本上解释了你需要做什么,但我会为你明确的布局代码。在 testAppDelegate.h
中:
This is really simple, and Chuck's comments basically explain what you need to do, but I will lay out the code explicitly for you. In testAppDelegate.h
:
@interface testAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
// You can make an IBOutlet to any kind of object you
// want; it's just a way for you to get a reference
// in code to an object that has been alloc'd and
// init'd already by the xib mechanism.
IBOutlet Controller *controller;
}
然后在InterfaceBuilder中进入你的xib,从测试应用程序委托对象到您的Controller对象(这些对象已经存在于xib中)。
Then go into your xib in InterfaceBuilder and hook up that outlet from your Test App Delegate object to your Controller object (these objects are already present in the xib).
在 testAppDelegate.m
:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// This is the key:
// _Don't_ alloc/init a new controller object. The
// objects in your xib are allocated and initialized
// by virtue of being in that file. You just need to
// give your AppDelegate a pointer to it, as above.
[controller setTextValue:@"hello"];
}
这篇关于从另一个类方法更新UI - Cocoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!