chuckieDub
chuckieDub

Reputation: 1835

Using a method in Objective-C cocoa

I'm having trouble with something very basic. I want to call this action:

- (IBAction)changeGreeting:(id)sender {
    self.userName = self.textField.text;

    NSString *nameString = self.userName;
    if ([nameString length]==0) {
        nameString = @"World";

    }
    NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
    self.label.text = greeting;

}

When the user presses return after entering text in the text field. This is what I have:

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField{
    if (theTextField == self.textField) {
        [theTextField resignFirstResponder];
        [changeGreeting];
    }
    return YES;
}

I'm not sure what to put where it says "changeGreeting." Think I'm missing the concept here. Thanks for any help.

Upvotes: 1

Views: 69

Answers (3)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Or if you want to track something from the textField then

[self changeGreeting:theTextField];

the sender will get theTextField as parameter if you need some processing on based on the textField.

Upvotes: 0

Omar Freewan
Omar Freewan

Reputation: 2678

you called the changeGreeting function in wrong way

you should call it like

[self changeGreeting:nil];

Upvotes: 1

DrummerB
DrummerB

Reputation: 40211

Your syntax is wrong. Try this:

[self changeGreeting:self];

In Objective-C the syntax for sending messages (calling functions) is like this:

[receiver message];

Since you implement the changeGreeting: method in the same class as you're calling it from, the receiver will be self. As the parameter (sender) you usually pass the object that sends the message, but since you don't use it in your implementation of changeGreeting: it doesn't really matter what you pass there.

Upvotes: 2

Related Questions