0

動作するランダムアプリがあります。10 UITextFields、a UILabel、 aで構成されUIButtonます。基本的な考え方は、にユーザー名を入力しUITextFieldsUIButtonランダムに名前を選択することです。うまく機能しますが、6人のユーザー名だけが参加している場合、他の4人はUITextFieldsどうでしょうか。ランダムリストまたは配列からそれらを除外するにはどうすればよいですか?この件を調査していただきありがとうございます。

これが私のmファイルです:

     #import "ViewController.h"

    @interface ViewController ()
    - (IBAction)random:(id)sender;
     @property (weak, nonatomic) IBOutlet UITextField *naam;
    @property (weak, nonatomic) IBOutlet UILabel *label;
    @property (weak, nonatomic) IBOutlet UITextField *naam2;
    @property (weak, nonatomic) IBOutlet UITextField *naam3;
    @property (weak, nonatomic) IBOutlet UITextField *naam4;
    @property (weak, nonatomic) IBOutlet UITextField *naam5;
    @property (weak, nonatomic) IBOutlet UITextField *naam6;
    @property (weak, nonatomic) IBOutlet UITextField *naam7;
    @property (weak, nonatomic) IBOutlet UITextField *naam8;
    @property (weak, nonatomic) IBOutlet UITextField *naam9;
    @property (weak, nonatomic) IBOutlet UITextField *naam10;


    @end  

    @implementation ViewController

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

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

    - (IBAction)random:(id)sender 
    {
        int text;
        text = rand() %10;
        switch (text) {
              case 0:
                    self.userName = self.naam2.text;
                    break;
              case 1:
                    self.userName = self.naam.text;
                    break;
              case 2:
                    self.userName = self.naam3.text;
                    break;
              case 3:
                    self.userName = self.naam4.text;
                    break;
              case 4:
                    self.userName = self.naam5.text;
                    break;
              case 5:
                    self.userName = self.naam6.text;
                    break;
              case 6:
                    self.userName = self.naam7.text;
                    break;
              case 7:
                    self.userName = self.naam8.text;
                    break;
              case 8:
                    self.userName = self.naam9.text;
                    break;
              case 9:
                    self.userName = self.naam10.text;
                    break;
              default:
                    break;
         }

         NSString *nameString = self.userName;
         if ([nameString length] == 0 ) {
             nameString = @"Wie?";
         }

         NSString *random = [[NSString alloc]
                    initWithFormat: @"De Bob is....%@!", nameString];
         self.label.text = random;
    }

    - (BOOL)textFieldShouldReturn: (UITextField *)theTextField {
        if (theTextField == self.naam) {
            [theTextField resignFirstResponder];
        } else if (theTextField == self.naam2) {
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam3) { 
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam4) {
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam5) {
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam6) {
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam7) {
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam8) {
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam9) {
            [theTextField resignFirstResponder];   
        } else if (theTextField == self.naam10) {
            [theTextField resignFirstResponder];   
        }
        return YES;
    }

    @end
4

3 に答える 3

0

for in ループを実行し、そのラベルのテキストが次のように空かどうかを確認してください: int count = 0; for(UITextField* textf in [self.view subviews]{ if(textf isKindOfClass:[UITextField class]){ if(![textf text] isEqualToString:@""]{ [textf setText:@"Some string"]; textf .tag = カウント; カウント++; } ] }

この後、乱数を取得するだけです: NSUInteger index = arc4random() % [array count];

10 個の乱数を取得したい場合は、それを for ループに入れてください。

for(int i = 0; i < 10; i++){
    NSUInteger index = arc4random() % [array count];
    //Loop through text fields again and get the one with the tag that matches the random number
}

for in ループは、通常の for ループのようにイテレータを使用する必要がなく、繰り返し処理するオブジェクトの数を知る必要がないため、非常に便利です。それはすべてあなたのために世話をしています。

于 2013-03-05T15:53:50.077 に答える
0

UITextField ランタイムを作成する必要があります。

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
textField.delegate = self;
[self.view addSubview:textField];

これにより、不要な textFields が存在しないことが保証されます。

または、viewDidLoad メソッドで、それぞれ非表示を YES に設定します。そして、プレーヤーの数に基づいて、非表示を NO に設定します。

于 2013-03-05T15:37:32.147 に答える
0

実装に応じて、入力されたユーザー名の値のみを含むNSMutableArrayorを最初に作成し、それを使用して配列から名前を選択することをお勧めします。NSArrayrandomIndex = arc4random() % [theArray count];

以下は可能な実装ですが、最適ではありません。

NSMutableArray *usernamesList = [NSMutableArray array];

for (UIView *view in [self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)view;
        if (![textField.text isEqualToString:@""]) {
            [usernamesList addObject:view];
        }
    }

    NSUInteger index = arc4random() % [usernamesList count];
    NSLog(@"The chosen name is %@", [usernamesList objectAtIndex:index]);
}

ユーザー名のテキストフィールドのみを反復できるようにするには、より良い解決策を使用することですIBOutletCollection

于 2013-03-05T15:41:48.827 に答える