2013-03-05 27 views
0

我有一个工作的随机应用程序。它由10个UITextFields,一个UILabel和一个UIButton组成。 基本思路是填写UITextFields的用户名并通过UIButton随机选择一个名字。它运作良好,但如果只有6个用户名加入,那么其他4 UITextFields呢?我怎样才能排除它们的随机列表或数组? 我希望你能帮助我,谢谢你对这件事的看法!如何仅在UILabel中显示随机填充的文本字段?

这里是我的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 
+0

欢迎来到stackoverflow。请确保你正确地标记你的问题,不仅仅是这样,它可以帮助你得到最好的答案,但在将来帮助其他用户。同时检查你的代码格式我已经改正了这个最好的可能性,使它更具可读性,只要确保我们下次可以阅读它。我努力理解你的代码片断,因为它们格式错误。此外,如果您找到帮助您解决问题的答案,请接受用户喜欢在那里获得功劳。 – Popeye 2013-03-05 16:16:28

回答

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。

+0

感谢您的回答Anoob,我是一个非常新手在obj C,所以原谅我,如果我不明白如何创建UITextfields运行时,你有一个例子吗? – jOnoMato 2013-03-12 10:52:10

+0

查看更新的答案。 – 2013-03-12 11:03:43

+0

再次感谢Anoop为您的快速回复,我会让你知道如果这个工程,问候Jonomato – jOnoMato 2013-03-12 14:31:20

0

我可以建议你首先创建一个NSMutableArrayNSArray,具体取决于实现,只包含填充的用户名的值,然后使用randomIndex = 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

+0

为了更好的随机重分区,首选使用'arc4random_uniform()'。 – Zaphod 2013-03-05 15:57:20

+0

谢谢你的回答Zaphod,既然你的建议需要一些时间来测试,我可以稍后给你结果 – jOnoMato 2013-03-12 10:43:24

0

只是做一个对循环并检查该标签的文本是否为空,如下所示: int count = 0;如果(textf isKindOfClass:[UITextField class]){(text [textf text] isEqualToString:@“”] {0121} {textf setText:@“一些字符串“]; textf.tag =计数; 计数++;} ] }

在这之后,只得到一个随机数: NSUInteger指数= arc4random()%[阵列计数];

如果你想得到十个随机数字,只要把它放在一个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 
} 

对于in循环来说非常方便,因为您不必为迭代器设置正常的循环,而且您也不必知道迭代的对象的数量。这一切都照顾你。

+0

谢谢你的回答,影子。我尝试过但失败了。我必须承认我缺乏对Xcode的理解,因此我在这方面很新手。 – jOnoMato 2013-03-12 10:49:22

+0

在这里,我会用一些更多示例代码编辑我的答案,以便您更好地了解要做什么。 – barndog 2013-03-12 17:06:10

相关问题