Reputation: 586
I'm working on an iOS application that involves users filling out text fields and navigating through views. Currently, the navigation is handled sometimes using the navigation bar, and sometimes through buttons. I tried to make some of the fields required: preventing progress through the application if those fields are left blank. This worked for the button based navigation but not for the Navigation Bar. My code is as follows:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"birthPopBus"]) {
currentPopoverSegue = (UIStoryboardPopoverSegue *)segue;
pvc = [segue destinationViewController];
[pvc setDelegate:self];
}
else if([[segue identifier] isEqualToString:@"SecondBusPageSegue"]) {
//This code breaks the next page
//Check fields, and create error message (code removed for readability)...
//If the fields are not filled in, display the alert with generated string.
if(!(first && last && email && phone && address && zip && ssn && birthFilled)){
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Required Fields Missing:"
message:alertMessageMutable
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
//Perform the segue, should only run if all required fields are filled.
else{
[self fillBus1Dictionary];
NSLog(@"application dictionary: %@", self.application);
SecBusPage * secondBusPage = segue.destinationViewController;
secondBusPage.application = self.application;
}
}
}
The error message will show up, but only AFTER the view has changed. So the user gets to SecondBusPage, and gets an alert saying "You didn't fill out fields X, Y, Z." which is very confusing for them. Any ideas for preventing the navigation bar from switching views?
Thanks in advance!
Upvotes: 2
Views: 274
Reputation: 1997
You can easily achieve this by implementing conditional segues like the one in my answer in the below linked post
Conditional Segue Using Storyboard
You cannot cancel a segue from prepareForSegue. Instead of that perform segues based on condtions
Upvotes: 3
Reputation: 4271
Storyboards limit flexibility in customizing behaviour.
What I did to take control of the navigation in my app was:
Create an id
instance object (property) with name mainDelegate
in the AppDelegate
of the project, and every time a viewController
is presented, I set it to the current view controller using the following in the viewDidAppear
method:
appDelegate.mainDelegate = self
I used a custom navigationBar
for the navigationController
.
In this custom navBar's class, on user's tap on a button, i check:
if ([appDelegate.mainDelegate isKindOfClass:[myViewController class]]){
if ([appDelegate.mainDelegate allFieldsAreFilled]){
//perform action of pushing view
}
}
allFieldsAreFilled
is a delegate method of myViewController
.Upvotes: 0
Reputation: 6918
You are doing the segue from the storyboard and you cannot cancel a storyboard segue. What you should do is delete that segue and use:
if(!(first && last && email && phone && address && zip && ssn && birthFilled)){
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Required Fields Missing:"
message:alertMessageMutable
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}else
{
[self.navigationController pushViewController:yourViewController animation:YES];
}
Upvotes: 0