无法让键盘消失或无法输入以注册



我正在尝试制作一个猜谜游戏,但由于某种原因,当我从另一个文件传输输入时,我无法注册输入。键盘也不会消失。缺少什么?

游戏视图控制器.h :

#import <UIKit/UIKit.h>
@interface GameViewController : UIViewController
<UITextFieldDelegate>
{
    int randomNum;
    int turns;
    id mons;
    IBOutlet UITextField *guessField;
    IBOutlet UILabel  *resultLabel;
    IBOutlet UILabel  *playAgain;

}
-(IBAction)enterGuess:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *image;


@end

游戏视图控制器.m

#import "GameViewController.h"
@interface GameViewController ()
@end
@implementation GameViewController
- (void)viewDidLoad
{
    turns = 0;
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)updateLevel:(UISegmentedControl *)sender {
    guessField.text = [NSString stringWithFormat:@" "];
    resultLabel.text = [NSString stringWithFormat:@" "];
    playAgain.text = [NSString stringWithFormat:@" "];
    _image.image=[UIImage imageNamed:@"guess-number.jpg"];
    turns = 0;
    if( guessField.text.length != 0 )
    {
        randomNum = arc4random() % 10;
    }
    else if ( guessField.text.length == 0 )
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"you did not enter a guess!" message:nil delegate:self cancelButtonTitle:@"try again" otherButtonTitles:nil, nil];
        [alert show];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)enterGuess:(id)sender{
    if( guessField.text.length != 0 )
    {
        if ([guessField.text intValue] != randomNum) {
            if ([guessField.text intValue] > randomNum) {
                _image.image=[UIImage imageNamed:@"arrow-down.png"];
                resultLabel.text = [NSString stringWithFormat:@" "];
                playAgain.text = [NSString stringWithFormat:@" Try Again. "];
                turns ++;
            }
            else if ([guessField.text intValue] < randomNum){

                _image.image=[UIImage imageNamed:@"arrow_up.png"];
                resultLabel.text = [NSString stringWithFormat:@" "];
                playAgain.text = [NSString stringWithFormat:@" Try Again. "];
                turns ++;
            }

        }
        else if ([guessField.text intValue] == randomNum){
            turns ++;
            if([guessField.text intValue] == 0){
                mons = [NSString stringWithFormat:  @"which I guess is no pokemon at all. Sad day!"];
            }
            if([guessField.text intValue] == 1){
                _image.image=[UIImage imageNamed:@"bulbasaur6.jpg"];
                mons = [NSString stringWithFormat:@"Bulbasaur"];
            }
            if([guessField.text intValue] == 2){
                _image.image=[UIImage imageNamed:@"ivysaur.jpg"];
                mons = [NSString stringWithFormat:@"Ivysaur"];

            }
            if([guessField.text intValue] == 3){
                _image.image=[UIImage imageNamed:@"venusaur.jpg"];
                mons = [NSString stringWithFormat:@"Venusaur"];
            }
            if([guessField.text intValue] == 4){
                _image.image=[UIImage imageNamed:@"charmander.jpg"];
                mons = [NSString stringWithFormat:@"Charmander"];

            }
            if([guessField.text intValue] == 5){
                _image.image=[UIImage imageNamed:@"charmeleon.jpg"];
                mons = [NSString stringWithFormat:@"Charmeleon"];

            }
            if([guessField.text intValue] == 6){
                _image.image=[UIImage imageNamed:@"charizard.jpg"];
                mons = [NSString stringWithFormat:@"Charizard"];

            }
            if([guessField.text intValue] == 7){
                _image.image=[UIImage imageNamed:@"squirtle.jpg"];
                mons = [NSString stringWithFormat:@"Squirtle"];

            }
            if([guessField.text intValue] == 8){
                _image.image=[UIImage imageNamed:@"wartortle.jpg"];
                mons = [NSString stringWithFormat:@"Wartortle"];

            }
            if([guessField.text intValue] == 9){
                _image.image=[UIImage imageNamed:@"blastoise.jpg"];
                mons = [NSString stringWithFormat:@"Blastoise"];

            }
            resultLabel.text = [NSString stringWithFormat:@"Congrats, you got pokemon # %i, %@. ", randomNum, mons];
            playAgain.text = [NSString stringWithFormat:@"Enter New Number to Reset and Play Again"];
            randomNum = arc4random() % 10;
            turns = 0;
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"you did not enter a guess!" message:nil delegate:self cancelButtonTitle:@"try again" otherButtonTitles:nil, nil];
            [alert show];
        }
    }}
@end

您缺少UITextFieldDelegate方法。 例如

-(void)textFieldDidEndEditing:(UITextField *)textField {
   [textField resignFirstResponder];
}

此外,未输入输入的事实似乎表明您没有将控制器设置为文本字段的delegate

若要检查是否已正确设置委托,请在 textFieldDidBeginEditing: 中放置断点。如果未调用,则表示尚未根据需要设置委托。另外,尝试在文本字段上放置一个调用resignFirstResponder的按钮。在textFieldDidEndEditing:中,您可以从字段中获取文本并对其进行处理。

尝试添加

[self.guessField resignFirstResponder];

enterGuess方法。

最新更新