如何在方法中暂停执行,直到用户发送“退出时结束”(按下键盘上的“下一步”按钮)?

时间:2012-07-22 16:39:21

标签: iphone objective-c ios

这就是我所拥有的:

while(wordList){                  //wordlist is instance of NSArray containing NSStrings
    word.text = [wordList objectAtIndex:x]; //word is instance of UILabel

    //LOOKING TO PLACE WAIT CODE HERE TO WAIT FOR "DID END ON EXIT"

    input = inputBox.text; //input is instance of UITextField

    [self compare:input andb:word.text]; //compare is an instance method to compare the two strings
    x++;
}

我是初学者,如果有人能帮助我,那就太棒了。

...最佳 SL

2 个答案:

答案 0 :(得分:0)

这是你在找什么?:

[yourTextField addTarget:self 
                action:@selector(yourMethod:)
                forControlEvents:UIControlEventEditingDidEndOnExit];

修改

为什么不打破while循环并在用户点击下一个时调用单独的方法?无需暂停并触发其他方法。

答案 1 :(得分:0)

你问的方法是行不通的。如果您的应用程序在方法中间暂停执行,会导致它取消暂停?

用户不会在循环中间输入文本 - 在循环开始执行之前必须已经完成。触发包含@Imirak建议的循环的方法,或者从其他一些用户交互中触发,例如按下按钮。这样你就会知道用户已经输入了文本,你编写的代码应该按预期工作。

但需要注意的一点是:您显示的代码不会检查compare:andb:方法的返回值。您还没有提供足够的信息来确定您希望该方法做什么来确定这是否有意义。

另外,你的循环控制逻辑是不正确的 - 因为写入它要么是无限循环,要么永远不会输入,这取决于wordListnil。考虑使用快速枚举语法而不是编写while循环,例如:

for (UILabel *currLabel in wordList)
{
    // It appears as though this may be the comparison you want to do
    // but there's not enough context in your question to be sure.
    //
    if ([inputBox.text isEqualToString:currLabel.text])
    {
        // Do something here. Again, it's not clear what you're trying to do.   
    }
}