如何防止未填充的UITextField显示在随机计数上? Xcode中

时间:2013-06-11 16:44:14

标签: xcode uitextfield nsarray arc4random

我正在开发一个应用程序,其中一个视图控制器中有五个 UITextFields ,用户可以填写他们想要的文本字段,当他们按下 UIButton 时将通过UILabel在第二个视图控制器上获得随机答案

我到目前为止已经开始工作,但是假设用户只填充前两个UITextFields ,他们得到的随机答案来自空白,未填充的UITextField

我的问题是:我如何开展工作,以便未填充的UITextFields 不属于随机数?这可能吗?

以下是代码:

FifthViewController.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];


}

谢谢!

1 个答案:

答案 0 :(得分:1)

SixthViewController

在将字符串添加到self.choiceAnswers:

之前,您可以检查字符串是否为空
if(![self.stringFromChoice1 isEqualToString@""]);
{
    [self.choiceAnswers addObject:self.stringFromChoice1];
}

确保您为self.choiceAnswers使用NSMutable数组。