我正在开发一个应用程序,其中一个视图控制器中有五个 UITextField,用户可以填写他们想要的文本字段,当他们按下UIButton时,他们将通过 UILabel 在第二个视图控制器上获得随机答案。
到目前为止我已经让它工作了,但是假设用户只填充了前两个 UITextFields,他们得到的随机答案来自一个空白的、未填充的 UITextField。
我的问题是:如何使未填充的 UITextFields不属于随机计数的一部分?这可能吗?
这是代码:
第五视图控制器.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.choice1.delegate = self;
self.choice2.delegate = self;
self.choice3.delegate = self;
self.choice4.delegate = self;
self.choice5.delegate = self;
}
- (IBAction)choicebutton:(id)sender {
SixthViewController *SVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SixthViewController"];
SVC.stringFromChoice1 = self.choice1.text;
SVC.stringFromChoice2 = self.choice2.text;
SVC.stringFromChoice3 = self.choice3.text;
SVC.stringFromChoice4 = self.choice4.text;
SVC.stringFromChoice5 = self.choice5.text;
[self presentViewController:SVC animated:YES completion:nil];
}
SixthViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.choiceAnswers = @[self.stringFromChoice1,
self.stringFromChoice2,
self.stringFromChoice3,
self.stringFromChoice4,
self.stringFromChoice5];
int index = arc4random() % [self.choiceAnswers count];
self.choiceanswer.text = self.choiceAnswers[index];
}
谢谢!