Dave C
Dave C

Reputation: 399

Objective C Pointers

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.

Upvotes: 1

Views: 190

Answers (3)

Paul
Paul

Reputation: 2738

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.

Upvotes: 0

jhleath
jhleath

Reputation: 804

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;

Upvotes: 0

unbeli
unbeli

Reputation: 30248

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.

Upvotes: 3

Related Questions