Vidya Bansal
Vidya Bansal

Reputation: 231

Can a Bar Button item connect to both IBAction and StoryBoard segue to a modal view controller at the same time?

I have a bar button item called 'Done'(system button) and I have a segue from this button to push a modal View controller. I have some data fields which can not be empty before the modal VC is pushed. So I have an IBAction attached to done button. In IBAction method, if the user presses on 'Done' button and data is empty an alertview is displayed else I call performSegueWithIdentifier to push the modal view controller.

When the app is run in simulator mode, pressing on 'DOne' button pushes the modal VC even though the data is empty. My IBAction method is not called. But if I delete the Segue, then run the app, alert view is displayed with correct message.

Is it not possible to have both IBAction/StoryBoard Segue on a Bar Button item ? Below is my code. I have created the modal kind of segue in storyboard with identifier "NewRelationDoneToEditRelationVCSegue".

- (IBAction)newViewControllerDoneButtonPressed:(id)sender 
{     
   if (self.groupSelected == NULL)
 {
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Select Group" message: @"Please Select a Group for your Contact" delegate: self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   alert.alertViewStyle = UIAlertViewStyleDefault;
   [alert show];
 } 
else 
{
   NSLog(@"test");
   [self performSegueWithIdentifier:@"NewRelationDoneToEditRelationVCSegue" sender:self];
}

Upvotes: 1

Views: 2649

Answers (2)

Vidya Bansal
Vidya Bansal

Reputation: 231

The problem is resolved. I deleted the segue from 'Done' button to the modal View Controller. Instead created a segue by CTRL-dragging from the leftmost small icon at the base of the origin view controller to the modal view controller. Basically created a manual segue. Its's working, so bar button item cannot have both IBAction / segue linked to it. It's either one. You have to create a manual segue to execute the IBAction and then push on another view controller. You can refer to [this question] also ( Why a bar button item on a NavigationViewController won't trigger an IBAction "

Upvotes: 2

Tommy Devoy
Tommy Devoy

Reputation: 13549

I'm assuming its just because your groupSelected bool just isn't equal to NULL. Bools default to no when the view loads unless initialized to yes. Change your if statement to check if its equal to no. Then just set it to yes when somebody clicks one of your things. Good to go.

Upvotes: 1

Related Questions