NSTextField not responding - objective-c

I created an NSTextField in my code:
.h :
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSTextField *numberOfConnectionsTextField;
}
#property (nonatomic, retain) NSTextField *numberOfConnectionsTextField;
.m :
#synthesize numberOfConnectionsTextField;
I change the value of the field here:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[numberOfConnectionsTextField setStringValue:#"0"];
}
And the value successfully changes. However, when I try to the change the value here:
- (void)connectionNumber:(NSString *)number {
[numberOfConnectionsTextField setStringValue:number];
NSRunAlertPanel(#"", number, #"", #"", #"");
}
The NSTextField didn't change. BUT, the NSRunAlertPanel did trigger with the 'number' string correctly.
Any ideas? I can't seem to find what I did wrong...

Your code looks quite good. There is no error, the first reason is always of not connecting the outlet to the object but as you are able to change in applicationDidFinishLaunching:, so that is not the issue here.
But You did not mentioned about "-(void)connectionNumber:(NSString *)" is this in the same class? or you have created same iboutlet in another class and missed something?
Or you manually released the outlet numberOfConnectionsTextField ? Please check if it is not null? like
if(numberOfConnectionsTextField){
NSLog(#"Still exist")
}
Another reason might be, from which method are you calling "-(void)connectionNumber:(NSString *)", many a times it is seen that people call set/get of outlet from init.
Please comment if you are doing every thing correct...

It's probably safer to use self.numberOfConnectionsTextField then the variable name numberOfConnectionsTextField alone. This kind of error happened to me when I first learn objective c. Some of the IBOutLet gone nil after a period of time.
Try to use self.variablename for strong , synthesize variable

Related

Interface variable declaration: synthesizing and unknown selector

I'm having a confusing problem. I'm only ankle deep in Objective-C so I'll try my best to explain. I have a class, which is a controller that simply declares an NSTextField which is in a nib file.
Here's its declaration in the interface file:
#property (nonatomic, retain) IBOutlet NSTextField *textField;
and in the implementation:
#synthesize textField;
Simple right? But if I call [textField stringValue] on it later on by means of clicking on a submit button then it fails with an unknown selector message (typical if it thinks it can't call that message on that object type). This looked like this:
-(IBAction)send:(id)sender {
NSString* txt = [textField stringValue];
[server send:txt];
}
To fix this, I did the following:
#interface MyController : NSObject {
NSTextField *textField;
}
I've not seen any tutorial/example showing this. They do it without declaring the NSTextField in this section of the interface.
My question is, why in my case do I have to declare it in the interface for it to work?

is it right way in obj-c programming

i wonder if the way i programm is right way or not. can you help me?
#interface ViewController : UIViewController
{
UILabel *messageLabel;
}
#end
when i declare new object in .h and create in .m i have to use #property (nonatomic, retain) UILabel *messageLabel? i saw few listings where when object are created by code #property doesn't exist and few where does exist and i'm confused.
is it correct when i don't use #property (in example for UILabel, UIImageView, UIButton) when i create objects by code?
#property is a key word useful to create setter/getter methods automatically for that field. If you don't need to access your label from outside your view controller you won't need to use #property and your code is fine.
Your code is correct. #property is used globally throughout your app, rather than being specific to your UIViewController. If you are only planning on referring or changing the label/other component define it within the { } otherwise #property should be used

Cocoa / Objective-C: Access a (Boolean) variable in different Classes

Situation:
Noob / Xcode 3.1
I have an AppView (NSView subclass) and an AppController (NSObject subclass)
in AppView.h i declare a boolean (BOOL: booleanDraw), which i set to 'NO' in AppView.m
When a button is clicked it 'launches' an action (AppController .h/.m) now i want to change booleanDraw to YES when the button is clicked.
I searched and found: do it with #property okay i tried to do that but it didnt work. (because i didnt totally get what to do probably)
i did:
#property BOOL booleanDraw;
(in AppView.h)
#implementation AppView
#synthesize(readwrite, nonatomic) booleanDraw;
(in AppView.m)
AppView *obj;
obj.booleanDraw = YES; // implicitly calls [obj setVar:3]
(in AppController.m)
Thanks for any help, i read some tutorials already but often they suggest some steps that should be basic but that dont belong to my repertoire, and the ADN often confuse me xD sorry but believe me im trying^^
You just reversed the synthesize and property statements:
in .h:
#property (nonatomic) booleanDraw;
(by default properties are readwrite, you only need to state when they are readonly)
in .m:
#synthesize booleanDraw;
In the controller you need to get the app view reference, the code you posted would not work unless you set "obj" to something.

Why "cannot use an object as a parameter to a method"?

I have the following ViewController class
#import <UIKit/UIKit.h>
#interface SampleViewController : UIViewController {
IBOutlet UITextField *field1;
}
#property (nonatomic, retain) UITextField *field1;
- (IBAction) method1:(id)sender;
#end
When I change the method1:(id)sender to method1:(UITextField)sender, I get the error "Cannot use an object as a parameter to a method".
I searched and found this post which says "it [using an object as a method parameter] is not a good idea in Objective-C because Objective-C does not allow statically allocated object".
Can anyone point out where I can find a more detailed explanation for this?
Thank you.
You're not passing a pointer of UITextField.
method1:(UITextField)sender
should be
method1:(UITextField *)sender
Objective-C doesn't like it when you pass non-pointers for object types.

Why am I getting an error for this?

Why am I getting these errors?
alt text http://img39.imageshack.us/img39/2203/help.tif
It says:
Error: Request for member "jokeTableView" in something not a struction or union
What does that mean? And why is it breaking. I tried reading about initWithStyle but I just could catch up on it
Here is my .h file:
#import <UIKit/UIKit.h>
#interface TableViewController : UITableViewController {
NSMutableArray *jokes;
IBOutlet UITableView *jokeTableView;
}
#property (nonatomic, retain) NSMutableArray *jokes;
#end
Thanks!
Your object (TableViewController) has no property named jokeTableView.
In order to access jokeTableView with the special dot operator, it needs to be a property. Otherwise you have to access it using Key-Value-Coding compliant methods or directly using the -> operator (or just use it as an ivar and no reference to self):
jokeTableView.delegate = self;
or
self->jokeTableView.delegate = self;
or
[self jokeTableView].delegate = self;
or
#property (retain) UITableView *jokeTableView;
// later...
self.jokeTableView.delegate = self;
Also note, however, that you are setting an outlet in the initializer and this won't work. You'll have to set this in the -[TableViewController awakeFromNib] method since self->jokeTableView will be nil when the initializer is actually called (which happens in IB prior to serializing the object into the nib file).
Since you are doing this at init time, the outlets should be NULL, so this initialization shouldn't do anything. This should be done at awakeFromNib time at the earliest.