-2

I am creating a scrambled word game that consists of 4 words, and therefore 4 textfields.

When the user inputs a string that is the length of the correct word, I want to check if that sequence is equal to the actual word.

 If it is, I want to clear the text field and return "YES!".

 If it is not, I want to clear the text field completely so the user can try again. 

Example: If the actual word is "LOGIC" and the user enters "GOLIC" as his guess for the correct word, I want the text field to clear completely so the user can try again.

     If the actual word is "LOGIC" and the user enters "LOGIC", I want the text field to clear and display the string "YES!"

Any help is much appreciated!

4

3 回答 3

1

将此绑定到 textField 的 editingDidEnd 操作。

- (IBAction)testText:(id)sender
{
    if ([myTextField.text isEqualToString:@"Logic"]) {
        myTextField.text = @"Yes";
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"YES!" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
    }
    else
        myTextField.text = @"";
}
于 2013-05-10T01:19:48.570 回答
1

UITextField 有一个属性“text”,你应该使用它。要与 NSStrings 进行比较,请使用 isEqualToString 方法。

if([myTextField.text isEqualToString:actualWord]) {
    //display YES!
}
myTextField.text = @"";

顺便提一句。如果需要,您可以使用 UIAlertView 显示 YES:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"YES!" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
于 2013-05-10T01:11:54.097 回答
0

在 UITextField 所属视图的文件中(取决于您希望对该属性拥有的访问权限),您应该添加:

@property (weak, nonatomic) IBOutlet UITextField *txtField;

里面:

@interface SettingsViewController ()

//Other code

@end

您还可以将 UITextField 连接为 Storyboard 中的 Outlet:

  1. 按 Xcode 左上角的 Suit
  2. 从一侧选择 Storyboard,从另一侧选择您要从中访问 Outlet(属性)的类。
  3. 按下控制按钮并从 UITextField 拖动到该类中的界面块。

这是一个链接: http ://www.youtube.com/watch?feature=player_detailpage&v=xq-a7e_l_4I#t=120s

然后在您的代码中,您可以通过以下方式访问它:

[self usernameField].text

您可以通过以下方式进行检查:

if ([[self usernameField].text isEqualToString @"YOUR STRING"]) {
//Code
} else {
//Code
}
于 2013-05-10T01:23:31.790 回答