我有一个工作的随机应用程序。它由 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