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

时间:2022-04-10 21:33:30

I have followed the first steps tutorial from apple, and I have a working app that can read text and display it in a label on button click. When I hit enter the textfield is resigned and the keyboard disappears, and when I hit the button the changeGreeting method is invoked. I want to call the changeGreeting function in the function thats used when I hit enter which is textFieldShouldReturn.

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

I tried everything I could think of, and read a lot online but Im not sure how to deal with (id)sender as a param for example. How should I edit my code to call changeGreeting on textfield enter?

我尝试了所有我能想到的东西,并在网上阅读了很多但我不知道如何处理(id)发送者作为例子。如何编辑我的代码以在textfield上调用changeGreeting输入?

code below:

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

thanks in adv

谢谢你

1 个解决方案

#1


1  

You already have it. Just ignore the sender parameter:

你已经拥有它了。只需忽略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]
}

#1


1  

You already have it. Just ignore the sender parameter:

你已经拥有它了。只需忽略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]
}