0

I have two questions :

  • When I try to call a method from different class (this method modify a textfield after having checked a condition) the method is well called (the NSLog in statutInternet works) but the TextField isn't modified.. When I do it from the (IBAction)internet method it works .. Any solution ?

  • Why Xcode want me to called my variables (like internetTextfield) with a _ before it ?

WindowsController.h

#import <Cocoa/Cocoa.h>

@interface WindowController : NSWindowController
@property (assign) IBOutlet NSTextField *internetLabel;

- (void)statutInternet;
- (IBAction)internet:(id)sender;

@end

WindowsController.m :

#import "WindowController.h"
@implementation WindowController

- (IBAction)internet:(id)sender;
{
   [self statutInternet];
}

- (void)statutInternet;
{
    NSLog(@"Callfunctionworks");
    if (condition) {
    [_internetLabel setStringValue:@"TxtFieldWorks!"];
    }

}

I try to call the method statutInternet with this from another class :

WindowController *fenetre = [[WindowController alloc] init];
[fenetre statutInternet];
Alexandre
  • 38
  • 6

2 Answers2

1

When I try to call a method from different class it doesn't work:

That's because, You're making another WindowController instance using this code:

WindowController *fenetre = [[WindowController alloc] init];

This is another new separate instance of same class, which I guess you're not showing. So you want to take reference to the window that's already showing rather than making a new instance.

Why Xcode want me to called my variables (like internetTextfield) with a _ before it ?

That's because when you declare variable using @property it does three things:

  1. Makes an internal variable by adding the conventional underscore (_) to the start of the variable name. That's why you've _ as prefix of your variable.
  2. Makes a setter-getter methods.
  3. Takes the keywords you used (ie. assign, strong, weak) in account while implementing the setter-getters.

You can read a good discussion here: @property and retain, assign, copy, nonatomic in Objective-C

Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Ok thank you, I didn't know .. how can I call the existing instance without creating a new one ? – Alexandre Mar 27 '16 at 16:12
  • Actually you need to know where in your app you're first creating this instance, may be your app delegate? – Adil Soomro Mar 27 '16 at 21:47
  • Yes I think, (I started from an existing project : ASHStatusItemPopover https://github.com/adamhartford/ASHStatusItemPopover ) here is what I have in the AppDelegate : ASHStatusItemPopover *statusItemPopover = [[ASHStatusItemPopover alloc] init]; statusItemPopover.windowController = [[WindowController alloc] initWithWindowNibName:@"WindowController"]; – Alexandre Mar 28 '16 at 11:30
  • You need to have this reference in your other class. – Adil Soomro Mar 28 '16 at 12:58
0

The NSTextField (and all of the other UI items) is not created yet when you call statutInternet method.

When your window loaded, your views will be ready :

_fenetre = [[WindowController alloc] initWithWindowNibName:@"WindowController"];
[_fenetre showWindow:_fenetre.window];
[_fenetre statutInternet];
Baris Atamer
  • 700
  • 7
  • 13