用户在textfield中输入到数组

时间:2015-04-21 11:49:20

标签: ios objective-c

我对Xcode来说是全新的,而且一般来说编程也不是很好。所以请好好的!

我制作了一个标签更新的小游戏,因此每次按下按钮都会出现挑战。我用开关盒完成了这件事。

我想要这个游戏的另一个小功能,用户可以在文本字段中键入他们想要的任何挑战,文本字段写入数组或其他内容。必须在switch-case-part中添加新的挑战作为案例,因此当用户按下“挑战”按钮时,用户可能会遇到自己的挑战。

我曾尝试了很多东西,但没有什么能帮助我,所以我只是想知道,如果你们中有人有完整的答案吗?我的问题标有' //这是我的问题所在。有什么建议吗?'在.m。

对不起,如果其他人之前曾问过类似的问题。我无法找到它。

另外,我在找到这个问题的正确标题时遇到了一些麻烦?

谢谢!

//  ViewController.h



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIButton *GetChallenge;
@property (strong, nonatomic) IBOutlet UILabel *ShowChallenge;

@property (strong, nonatomic) IBOutlet UITextField *AddNewChallenge;
@property (strong, nonatomic) IBOutlet UIButton *AddNewButton;



@end

//  ViewController.m

#import "ViewController.h"


@implementation ViewController
@synthesize GetChallenge;
@synthesize ShowChallenge;
@synthesize AddNewChallenge;
@synthesize AddNewButton;







- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}



- (IBAction)AddNewButton:(id)sender {





    // Here is where my problem is. Any suggestions?
    // Here is where my problem is. Any suggestions?
    // Here is where my problem is. Any suggestions?
    // Here is where my problem is. Any suggestions?
    // Here is where my problem is. Any suggestions?




}




- (IBAction)GetChallenge:(id)sender {

    int text = rand() %93;


    switch (text) {
        case 0:
            ShowChallenge.text = @"a!";
            break;
        case 1:
            ShowChallenge.text = @"b!";
            break;
        case 2:
            ShowChallenge.text = @"c!";
            break;
        case 3:
            ShowChallenge.text = @"d";
            break;
        case 4:
            ShowChallenge.text = @"e!";
            break;
        case 6:
            ShowChallenge.text = @"f";
            break;
        case 7:
            ShowChallenge.text = @"g";
            break;
        case 8:
            ShowChallenge.text = @"h";
            break;
        case 9:
            ShowChallenge.text = @"i";
            break;
        case 10:
            ShowChallenge.text = @"j";
            break;
        case 11:
            ShowChallenge.text = @"k";
            break;
        case 12:
            ShowChallenge.text = @"l";
            break;
        case 13:
            ShowChallenge.text = @"m";
            break;
        case 14:
            ShowChallenge.text = @"n!";
            break;
        case 15:
            ShowChallenge.text = @"o";
            break;
        case 16:
            ShowChallenge.text = @"p";
            break;
        case 17:
            ShowChallenge.text = @"q";
            break;
        case 18:
            ShowChallenge.text = @"r";
            break;
        case 19:
            ShowChallenge.text = @"s";
            break;
        case 20:
            ShowChallenge.text = @"t";
            break;
        case 21:
            ShowChallenge.text = @"u";
            break;
        case 22:
            ShowChallenge.text = @"'v";
            break;
        case 23:
            ShowChallenge.text = @"'w";
            break;
        case 24:
            ShowChallenge.text = @"'x";
            break;
        case 25:
            ShowChallenge.text = @"'y";
            break;
        case 26:
            ShowChallenge.text = @"z";
            break;
        case 27:
            ShowChallenge.text = @"aa";
            break;
        case 28:
            ShowChallenge.text = @"bb";
            break;
        case 29:
            ShowChallenge.text = @"cc";
            break;
        case 30:
            ShowChallenge.text = @"dd";
            break;
        case 31:
            ShowChallenge.text = @"ee";
            break;
        case 32:
            ShowChallenge.text = @"ff";
            break;
        case 33:
            ShowChallenge.text = @"'gg!";
            break;
        case 34:
            ShowChallenge.text = @"'hh";
            break;
        case 35:
            ShowChallenge.text = @"ii";
            break;
        case 36:
            ShowChallenge.text = @"jj";
            break;
        case 37:
            ShowChallenge.text = @"kk";
            break;
        case 38:
            ShowChallenge.text = @"ll";
            break;
        case 39:
            ShowChallenge.text = @"mm";
            break;
        case 40:
            ShowChallenge.text = @"nn";
            break;
        case 41:
            ShowChallenge.text = @"oo!";
            break;
        case 42:
            ShowChallenge.text = @"pp";
            break;
        case 43:
            ShowChallenge.text = @"qq";
            break;
        case 44:
            ShowChallenge.text = @"rr";
            break;
        case 45:
            ShowChallenge.text = @"ss";
            break;
        case 46:
            ShowChallenge.text = @"tt";
            break;
        case 47:
            ShowChallenge.text = @"uu";
            break;
        case 48:
            ShowChallenge.text = @"vv";
            break;
        case 49:
            ShowChallenge.text = @"ww";
            break;


            //cartgame

        case 50:
            ShowChallenge.text = @"aaa";
            break;
        case 51:
            ShowChallenge.text = @"bbb!";
            break;
        case 52:
            ShowChallenge.text = @"ccc";
            break;
        case 53:
            ShowChallenge.text = @"ddd!";
            break;
        case 54:
            ShowChallenge.text = @"eee!";
            break;
        case 55:
            ShowChallenge.text = @"fff";
            break;
        case 56:
            ShowChallenge.text = @"ggg";
            break;
        case 57:
            ShowChallenge.text = @"hhh!";
            break;
        case 58:
            ShowChallenge.text = @"iii";
            break;
        case 59:
            ShowChallenge.text = @"jjj";
            break;
        case 60:
            ShowChallenge.text = @"kkk";
            break;
        case 61:
            ShowChallenge.text = @"lll";
            break;
        case 62:
            ShowChallenge.text = @"mmm";
            break;
        case 63:
            ShowChallenge.text = @"nnn";
            break;
        case 64:
            ShowChallenge.text = @"ooo";
            break;
        case 65:
            ShowChallenge.text = @"ppp";
            break;
        case 66:
            ShowChallenge.text = @"qqq";
            break;
        case 67:
            ShowChallenge.text = @"sss";
            break;
        case 68:
            ShowChallenge.text = @"ttt";
            break;
        case 69:
            ShowChallenge.text = @"uuu";
            break;
        case 70:
            ShowChallenge.text = @"vvv";
            break;
        case 71:
            ShowChallenge.text = @"www";
            break;
        case 72:
            ShowChallenge.text = @"xxx";
            break;
        case 73:
            ShowChallenge.text = @"yyy";
            break;
        case 74:
            ShowChallenge.text = @"zzz";
            break;
        case 75:
            ShowChallenge.text = @"aaaa";
            break;
        case 76:
            ShowChallenge.text = @"bbbb";
            break;
        case 77:
            ShowChallenge.text = @"cccc";
            break;
        case 78:
            ShowChallenge.text = @"dddd";
            break;
        case 79:
            ShowChallenge.text = @"eeee";
            break;
        case 80:
            ShowChallenge.text = @"ffff!";
            break;
        case 81:
            ShowChallenge.text = @"gggg!";
            break;
        case 82:
            ShowChallenge.text = @"hhhh";
            break;
        case 83:
            ShowChallenge.text = @"iiii";
            break;
        case 84:
            ShowChallenge.text = @"jjjj!";
            break;
        case 85:
            ShowChallenge.text = @"kkkk!";
            break;
        case 86:
            ShowChallenge.text = @"llll!";
            break;
        case 87:
            ShowChallenge.text = @"mmmm!";
            break;
        case 88:
            ShowChallenge.text = @"nnnn!";
            break;
        case 89:
            ShowChallenge.text = @"oooo";
            break;


        case 90:
            ShowChallenge.text = @"pppp";
            break;
        case 91:
            ShowChallenge.text = @"qqqq!";
            break;
        case 92:
            ShowChallenge.text = @"rrrr!";
            break;



        default:
            break;
    }




}



// Goodbye keyboard

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [AddNewChallenge resignFirstResponder];

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField){
        [AddNewChallenge resignFirstResponder];
    }
        return NO;

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end

再次感谢!

1 个答案:

答案 0 :(得分:0)

首先,我建议将所有挑战放入阵列中。它将使您的代码更清晰,更易于阅读,更易于维护。

然后,您只需将用户的挑战添加到阵列即可。

当谈到随机选择挑战时,您可以这样做:

int text = rand() % array.count;

//Check the value is a valid index:

if (text >= 0 && <= (array.count - 1))
{
  ShowChallenge.text = array[text];
}