0

我有一个工作的随机应用程序。它由 10 UITextFields、 aUILabel和 a组成UIButton。基本思路是在用户名中填写UITextFields并通过UIButton随机选择一个名称。它运作良好,但如果这个场合只有 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];

如果你想得到十个随机数,只需将其放入 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

我可以建议您首先根据实现创建一个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 回答
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];

这将确保那里没有不必要的文本字段。

或者,在 viewDidLoad 方法中将每个隐藏设置为 YES。并根据玩家的数量将隐藏设置为 NO。

于 2013-03-05T15:37:32.147 回答