1

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 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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;

6 Comments

huh? labels don't have stringValue, just text.
An NSTextField is a label. And, it has a stringValue. The documentation is here: developer.apple.com/mac/library/documentation/cocoa/reference/…
No setStringValue, and properties is not the opposite of target-action. No downvote, as the IBOutlet and lowercase comments are good points.
mylabel.text works in other areas on my app... I think it just calls the above method anyways
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'
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.