2

我遵循了苹果的第一步教程,并且我有一个工作应用程序可以读取文本并将其显示在按钮单击的标签中。当我点击输入时,文本字段被辞职并且键盘消失,当我点击按钮时,该changeGreeting方法被调用。我想changeGreeting在按回车时使用的函数中调用该函数textFieldShouldReturn

我尝试了我能想到的一切,并在网上阅读了很多内容,但我不确定如何将 (id)sender 作为参数处理。我应该如何编辑我的代码以在文本字段输入时调用 changeGreeting?

下面的代码:

- (IBAction)changeGreeting:(id)sender {

    self.userName = self.textField.text;

    NSString *nameString = self.userName;
    NSString *endString = @"burp";
    int r = arc4random() % 74;

    if ([nameString length] == 0) {
        nameString = @"World";
    }
    NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@, %@! Random String of numbers: %d", nameString, endString, r];
    self.label.text = greeting;
}

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textField) {
        [theTextField resignFirstResponder];
        // on enter the keyboard is removed, but I want the
        // changeGreeting method involed too, something like
        // [self changeGreeting]



    }
    return YES;
}

感谢广告

4

1 回答 1

1

You already have it. Just ignore the sender parameter:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textField) {
        [theTextField resignFirstResponder];
        // on enter the keyboard is removed, but I want the
        // changeGreeting method involed too, something like
        [self changeGreeting:nil]
}
于 2012-11-09T13:16:53.863 回答