Question

I spent quite a few hours on this but could not resolve it. I follow this basic tutorial to create cocoa mac os application that has

  • Text Control
  • Button control
  • Label Control

I created a new class, added NSString object, change the class name for NSString object to my class. Then I control+dragged text box, label to myclass.h (outlets). I did the same to button but this time selected action.

Now I put code in action to acess my label, I can't. They do not appear in intellesense. I have spent quite sometime on this. This is a very basic qeustion. what am I doing wrong? i am using XCODE 4.6.3.

------------------- .h file ---------------------
#import <Foundation/Foundation.h>

@interface myclass : NSObject
- (IBAction)doSomething:(id)sender;
@property (weak) IBOutlet NSView *mytextbox;
@property (weak) IBOutlet NSTextField *mylabel;

@end

----------------- .c file ---------------------------
#import "myclass.h"

@implementation myclass

- (IBAction)doSomething:(id)sender {
    [self.mytextbox.value setStringValue:@"hello there"];

    [mytextbox] (not accessible)
}
@end
---------------------------------------------
Was it helpful?

Solution 3

The trick here was, I had to use underscore character before the variable (property) name. I don't know how why underscore work with and what is its purpose. I am new to xcode. So

_mytextbox  //worked

OTHER TIPS

try to make it a NSTextField

@property (weak) IBOutlet NSTextField *mytextbox;

also fix it in your nib file.

It shouldn't be a .c file, it should have a .m extension. (or .mm if you want to use c++ code as well). This is not just a style thing, it is necessary.

[mytextbox] (not accessible)

To access the mytextbox instance variable, you do self.mytextbox or [self mytextbox].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top