views:

72

answers:

3

Hello,

I am really confused by pointers... I guess I am used to other languages that do not have them. I have the following snippet of code:

- (void)setAccountNumber:(NSString *)accountNumber Pin:(NSString *)pin {
 myAccessNumber.text = accountNumber;
 myPin.text = pin;
}

This function is being called by another view before this view takes control. No matter what I do, I can not get the value of account number into the access number label and the pin into the pin label. When I debug, I can see the values are correctly being passed in but they are still not showing up. Furthermore, my interface builder is all properly set up to receive input.

I have also tried this, it also does not work

- (void)setAccountNumber:(NSString *)accountNumber Pin:(NSString *)pin {
 myAccessNumber.text = [[NSString alloc] initWithString:accountNumber];
 myPin.text = [[NSString alloc] initWithString:pin];
}

Thanks for any help.

+3  A: 

Are you sure myAccessNumber and myPin are actually attached to your view? Aren't they nil when you debug?
Otherwise your first snippet is ok, no need for alloc/initWithString stuff.

unbeli
I figured out the problem... I think the view wasn't loaded yet so it was seeing the labels as "0x0" and saying the text attribute was "out of scope" I ended up setting a NSString temporarily then setting the labels text on the view load.
Dave C
A: 

You want this code:

[myAccessNumber setStringValue:accountNumber];
[myPin setStringValue:pin];

Those are the methods that you would use to put text into a label or text field. You can access the value of them by using [myLabel stringValue];.

While you can use dot-notation to access the properties of an object, I would say that it is more 'Cocoa-like' to use target-action notation.

You'll also want to make sure that the outlets IBOutlet NSTextField *myAccessNumber from your .h file are connected to the actual interface in Interface Builder.

Also, I wouldn't capitalize Pin in your method declaration as normally, Cocoa style says that you should keep the first word lowercase and CamelCase the rest of the words in a method declaration. (For each parameter).

For example:

- (void)myMethodWithString:(NSString*)newString andData:(NSData*)newData;
huntaub
huh? labels don't have stringValue, just text.
unbeli
An NSTextField is a label. And, it has a stringValue. The documentation is here: http://developer.apple.com/mac/library/documentation/cocoa/reference/ApplicationKit/Classes/NSTextField_Class/Reference/Reference.html
huntaub
No setStringValue, and properties is not the opposite of target-action. No downvote, as the IBOutlet and lowercase comments are good points.
Eiko
mylabel.text works in other areas on my app... I think it just calls the above method anyways
Dave C
that's true, but OP clearly uses something with text property, otherwise he'd get an exception. I guess it's UILabel from iPhone.
unbeli
Yes, like I said, they both work. I just prefer [myLabel setStringValue:string] as it matches with the rest of Objective-C.Looking into it, yes. The iPhone UILabel does _not_ have the setStringValue method. However, the post was not tagged with 'iPhone' or 'Cocoa Touch'
huntaub
A: 

If the variables myAccessNumber and myPin are actual labels in the window (UIOutlets) then you want to do:

- (void)setAccountNumber:(NSString *)accountNumber Pin:(NSString *)pin
{
  myAccessNumber.stringValue = accountNumber;
  myPin.stringValue = pin;
}

The stringValue method is defined on the parent of NSTextField: NSControl.

Paul