使用参数从兄弟方法中调用方法

时间:2012-11-09 13:13:56

标签: objective-c xcode ipad

我已经按照Apple的第一步教程,我有一个可以阅读文本并在按钮点击时在标签中显示它的应用程序。当我按Enter键时,文本字段被重新签名,键盘消失,当我按下按钮时,调用changeGreeting方法。我想在输入changeGreeting时使用的函数中调用textFieldShouldReturn函数。

我尝试了所有我能想到的东西,并在网上阅读了很多但我不知道如何处理(id)发件人作为例子。如何编辑我的代码以在textfield上调用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;
}

感谢adv

1 个答案:

答案 0 :(得分:1)

你已经拥有它。只需忽略sender参数:

- (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]
}