I am working on an app in which there are five UITextFields in one view controller, the user can fill the text fields they want and when they press a UIButton they'll get a randomized answer on a second view controller via a UILabel.
I got it to work so far, but let's say the user only fills the first two UITextFields and the random answer they get is from a blank, unfilled UITextField.
My question is: how do I make work so that the unfilled UITextFields are not part of the random count? Is this possible?
Here's the code:
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];
}
Thanks!
SixthViewController
You can check and see if a string is empty before adding it to self.choiceAnswers:
if(![self.stringFromChoice1 isEqualToString@""]);
{
[self.choiceAnswers addObject:self.stringFromChoice1];
}
Make sure that you are using an NSMutable array for self.choiceAnswers.