Refresh UI of another class method - Cocoa

I would like to update the interface in my application from AppDelegate, but whenever I call it like this:

Controller *object = [[Controller alloc] init];
[object methodHere];

It does not update the user interface. What am I doing wrong here? I set NSLog to find out if this is caused, and it does. Here is an example project that shows an error.

Edit: can someone just show me what I need to change in the project that I provided. I just don't know what to introduce into my project so that I can change the value of a simple NSTextField from another class.

+3
source share
3 answers

, , , . testAppDelegate.h:

@interface testAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    // You can make an IBOutlet to any kind of object you
    // want; it 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;

}

xib InterfaceBuilder Test App Delegate Controller ( 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"];
}
+4

[[Controller alloc] init], Controller, nib. , .

, Controller , NSArray. , nib, NSTextField, , - , , .

, , nib.

+9

, . , appDelegate.h, .

: , , ( ).

.h

Controller* myController;

, , myController , , ,

[myController methodHere];

, . myController, .

, , - , . ,

-(returnType)callDelegateToDoSomething;

-(returnType)callDelegateToDoSomething:(id) sender;

, .

[sender methodHere];

, , . (, . , .)

+3
source

Source: https://habr.com/ru/post/1793939/


All Articles