Storyboard: Condition segue transition for navigation (push) - iphone

I am new to storyboard and xcode (using 4.6)
I m using Navigation Storyboard with push segue.
I would like to implement a select snap view, that if have minimum 1 snap selected - the segue would work. Otherwise, the user should stay on the same view.
I created 2 UIView: SelectSnapsViewController and ShippingDetailViewController.
In SelectSnapsViewController.m I added the following:
- (IBAction)nextButtonPressed:(id)sender
{
looseDataAlert = NO;
[okkkBtn setFrame:CGRectMake(116, 272, 72, 37)];
if ([appDelegate.selImageDetails count] == 0)
{
label1.text = #"To proceed you need to add snaps.";
[self openAlertView];
}
else{
[self performSegueWithIdentifier:#"MySegue" sender:self];
}
}
When I debug the code - I see that the it always fall in else case of condition, however the segue (transition to the new view) still happens.
How can I prevent the user to be transferred to the new view?

There are two ways to implement conditional segues.
1) Configure the Segues as 'manual' from the ViewController and invoke performSegueWithIdentifier conditionally within your IBAction method that you wire up to event handling for your button.
OR 2) Configure the Segue as a 'Triggered Segue' from your button and implement - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender in your view controller to perform the conditional logic.

Are you saying that you only want to perform the segue if a condition is true?
If so, instead of creating the segue directly from a control or table cell, create a triggerless segue. A triggerless segue has the view controller as its source, and it won't ever fire automatically. Instead you can fire it programmatically any time you like, including from an IBAction.
To create a triggerless segue, start control+dragging the segue from the containing view controller icon in the scene dock at the bottom of the scene. Drag to the destination scene like normal, and pick the segue type. Select the segue, and in the inspector, choose a segue identifier.
At runtime, when you want to perform the segue, invoke -[UIViewController performSegueWithIdentifier:sender:]. You can pass any object you'd like for the sender, including nil. If the sender has no use to you, pass nil.
So, in summary:
Create a triggerless segue from the view controller to the destination scene
Set an identifier for the segue in the inspector
At runtime, and form code, call -[UIViewController performSegueWithIdentifier:sender:] when you want to trigger the segue.

Related

Why is a storyboard popover segue appearing on a subsequent view within a navigation controller

this is odd, but I'll try to explain the best I can.
I have a navigation controller which has a view. The view asks a simple question. That view then has two segues available -- one on the view for a correct answer, which is a "show" on the navigation controller and the other for an incorrect answer which is a "popover".
The segues are tied to the view and the answer box for the show and popover respectively.
I'm testing the answer on button press and using performSegueWithIdentifier to then show the appropriate window.
if (answerField.text == "2") {
println("Correct")
performSegueWithIdentifier("Correct", sender: sender)
} else {
println("Incorrect")
performSegueWithIdentifier("Error", sender: sender)
}
The problem I'm having is that if I get the answer correct, it moves fine to the next view, but it shows the "incorrect" popover view after segueing to the correct one.
I know that sounds complicated, but it's a super simple app at present. I suspect I'm just doing it wrong. Of note is that I also get an error "Presenting view controllers on detached view controllers is discouraged" which may be related.
Many thanks for any assistance anyone can provide.
D.
It sounds like you have one or both of the segues hooked up directly to the button in your storyboard, which will trigger the segue automatically on the button press even when you don't call performSegueWithIdentifier(_:) in code. You are then setting up an IBAction method which is also called on the button press, where you are then programmatically performing the segue again.
You can check this by going to your storyboard and selecting the button. Go to the connections inspector on the right, and I expect you'll see a Triggered Segues section, with an "action" triggering your "show" segue. You can remove this by clicking the cross next to the connection.
For segues that should be initiated programmatically, you should create a segue by dragging from the view controller object (rather from a control / actionable element) to the next view controller. This will create a generic segue that is only triggered by performSegueWithIdentifier(_:) in code.
As you have noticed, since your segue is for a popover presentation it will complain unless it is explicitly anchored to a particular view in your storyboard. You can still drag the segue from the view controller object to the popover view controller, but you must manually hook up the anchor: select the segue in the storyboard, choose the attributes inspector on the right, and drag from the circle in the "Anchor" field to the text field you want to anchor to.

Changing the behavior of UIBarButtonItem i.e shifting from action to segue and vice-a-versa

I have a UIBarButtonItems, left side is edit and right side is add/done;
Now When edit is not clicked the action for right side button is to take me to next view controller and I have setup this using segue in storyboard.
However when edit is clicked I want to perform a different action i.e I don't want to go to that new view controller rather disable editing when clicked on that right side UIBarButtonItem.
Now I also have a IBAction done; which disables the editing.
But how to shift the behavior between add/done;
I can do this via programming; But since I have added the segue via storyboard, I wanted to know if there is a way I can switch between segue from storyboard and the IBAction in my controller.
Thanks.
Your problem is that you connect button action to perform specific segue.
You can
create segue between your controllers by Ctrl+dragging from one VC to another (not from specific view to view controller). You can do it easily in VC list window (to the left of your Storyboard scene). Don't forget give IDs to your segues.
connect your button actions to VC action listeners
in these listeners use your logic and invoke with specific identifier
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
That's all!

Possible to have all cells segue to same view controller?

Instead of control-dragging every cell to the other view controller, is there a way to make them all segue to the view controller more easily? I would also need to add a Segue Identifier to the individual segues as well.
That is exactly why you have a tableview delegate method.
– tableView:didDeselectRowAtIndexPath:
whatever cell in the table this method will get invoked and there you can call for whichever transition like push,model etc programmatically in code.
This sounds like something you should do programmatically rather than in interface builder. You'll want to set the selection action for the cells to trigger the Segue.
You can create a manual segue from one view controller to another. Just select view controller from the bottom bar (where you see first responder and all) and ctrl drag to other view controller. Select the segue and give it a unique identifier (TransitionSegue in this example) from attributes inspector. From code call the segue whenever you want a transition.
[self performSegueWithIdentifier: #"TransitionSegue" sender: self];
Hope this helps.

Why a bar button item on a NavigationViewController won't trigger an IBAction

I have a pretty basic app I am working on that uses a navigation view controller system. One of my view is a table view that I added a bar button to in the story board. This bar button is just a basic add button for adding a new item to the table view. I identified the segue for use in my prepare for segue method and then I created an IBAction for the button as well.
The segue pushes to my next view just fine and passed my desired object, but the action never triggered. I ended up having to put the code for the IBAction in my prepare for segue method to get it to work.
I'm just wondering why IBAction was never triggered.
1) remove the segueNamedFoo you have created from the button to the destination view controller
2) create a manual segueNamedFoo push segue by CTRL-dragging from the leftmost small icon at the base of the origin view controller to the destination view controller
3) in your - (IBAction)doStuffBeforeSegue:(id)sender method which you have connected to your button perform the tasks which you want and then call [self performSegueWithIdentifier:#"segueNamedFoo"]

Add button in UITabBarController in Storyboard

I have the following storyboard:
In the tab bar controller I have a add button with a segue to a view controller. I need to use the method "prepareForSegue:" because I need to transfer an object but in the Repositories Table View doesn't fire the method.
How can I detect the method "prepareForSegue:" in the Repositories Table View? Is it possible?
I suppose for "detect the method prepareForSegue" you mean "identify the right segue in the prepareForSegue method".
You do this by using [segue identifier] and the string comparison method isEqualToString: to compare it to what you set the Identifier to be in storyboard.
Also, make sure that the button, table view cell, button or other element is attached to the right segue. You can of course do that in storyboard as well. If you need to do it in code e.g. because the button is created programmatically, you will have to call the segue yourself. In the button handler you just use
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];