Change view controller on press button - iphone

I have two View Controllers, I need to have a button in ViewControllerOne that when I press it Show me ViewControllerTwo.
In storyboard I related both views with a "Presenting Segues" - Push modal. And both views have a view controller class.

I'm not sure what you mean by "I related both views with a 'Presenting Segues' - Push modal." Are you using a navigation controller and want a push segue, or do you want to do a modal segue? A "push modal" is a contradiction in terms.
So, let's imagine that you want a modal segue. So, you put a button on the first view, right-click and drag (or control-click and drag) from that button to the second view.
You'll get a pop up asking for type of segue. Select "modal".
And you're done transitioning from 1 to 2. No code necessary.
If you want a button on the second view to take you back to the first view, you do not want a modal segue from the second view back to the first view, but rather you want to dismissViewControllerAnimated. You can do this via a custom segue, or easier, just have a button which calls dismissViewControllerAnimated. Thus, you'd add a button to the second view, and while the editor is in in "assistant" mode (where the associated .h file is showing below the interface builder; see below if you want to know how to show the .h file at the same time as the Interface Builder screen), right-click (or control-click) and drag from the button on the second view down to the second view controller's .h file:
By the way, if you don't see the .h file there, click on the "assistant" editor button and choose "automatic" for the files to be shown down there, and you should be good:
It will then show you a pop up asking you what you want to do. Select IBAction and give your new method a name:
Then go to your code for the view controller, and add the dismissViewControllerAnimated code:
All that code says (and in this example, I just called my IBAction dismissTwo) is:
- (IBAction)dismissTwo:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}

If you want to do a push segue, it's even easier. First, if you don't already have a navigation controller, add one by selecting the first view and choose "Embed in" - "Navigation Controller":
When you do this, you'll have a new navigation controller (which you don't really need to do much with) and the first view will have a navigation bar.
Now, right-click (or control-click) on your first view's button and drag over to the second view:
This time, select the "push" segue:
And you'll know that it worked, because your second view will have a navigation controller
You don't need a button to go back, because the navigation controller will automatically have a "Back" button, so you don't need to add your own.
This is how you achieve a push segue.

Related

Is Initial View Controller does work?

I have a viewcontroller and a tab bar controller. I want to change view controller as initial view controller and create a segue to show tab bar controller.
I have tried to set the new initial view controller. There are 2 ways. First, check the tick box "Is Initial View Controller". Second, drag the arrow to new screen. It does not show any error, but it shows the tab bar controller as usual and I can't find the view controller either.
Anyone know how to what is going on. Please tell me to fix. Thanks
You need to use a UINavigationViewController and connect it up in the hierarchy in a similar fashion to:
The leftmost view is the "view" that you mention, which needs to show the UITabViewController.
I've added a button for ease of use.
The next along (send from the left) is the UINavigationViewController. No settings on here need to be set etc... it's pretty much "plug and play" as to put it.
The next view (third from the left) is the UITabViewController (with corresponding views to it's immediate right).
The connection between the UINavigationViewController and the UITabViewController is "rootViewController" as the Tab View is the "root view controller" e.g. the primary view controller.
The segue between the first view, and the navigation view controller is merely a "show" segue. I liked directly between the button and the navigation controller in Xcode.
Hope this mash-up helps!
Edit I wrote a quick mask up of what I posted in the images for you to play with: https://dl.dropboxusercontent.com/u/61211034/Stackoverflow/NavgationViewController.zip
Based on your storyboard's image, looks like you don't have any NavigationController embedded to your root LoginViewController, try this:
Select your LoginViewController on your storyboard
Then in xcode Menu bar at the top, go to Editor -> Embed in -> Navigation Controller (as you can see in the image below)
Then your NavigationController will be set as your Initial View Controller, and see your LoginViewController be displayed and work as expected your segue.

Tab Bar mysteriously disappears?

Attached are two images. The first shows my current main.storyboard, and the second shows my problem when I run the app. I have a tab bar controller that has two tabs. On the first tab there is a button. When pressed, the button goes to another view controller with content. At the top is a Navigation bar with a back button. After viewing content, I press the back button, and am back on the original page with the button, but the tab bar is missing. I have seen a few answered questions, but it appears they made their tab bar in the view controller instead of the storyboard. Another says that I should never return to a previous view unless I use an unwind segue. Is this true? If so, how do I set up an unwind segue. If not, how do I fix this problem otherwise? Thank you.
http://i.stack.imgur.com/IYmX2.png
http://i.stack.imgur.com/7slt5.png
The problem is in the wiring of your ViewControllers. You have probably embedded your UITabBarController inside the UINavigationController and not the other way around.
A correct layout looks like this in Interface Builder :
To reproduce:
In Interface Builder drop a UITabBarController. This will come with 2 UIViewController's already wired in.
Pick one of the UIViewController's (let's call it VController1) and click on Editor / Embed in / Navigation Controller. This wires the VController1 to live inside a UINavigationController that is inside the UITabBarController
Add a 3rd UIViewController next to VController1 Let's call it VController3
Wire in a segue between VController1 and VController3, for example with a button.
I hope that's clear enough
Try Linking the button in your viewcontroller (other than the views of the tabbed bar controller) with the tabbed bar controller. Create a segue that links the button with the controller of the tabbed bar application

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!

performSegueWithIdentifier isn't working

I have this code running when I tap a button:
[self performSegueWithIdentifier:#"services" sender:self];
NSLog(#"ButtonClicked");
I can see the log message appearing each time I press the button but the segue isn't activating.
In my storyboard file I have a segue named services, that's the segue I want to activate.
Any idea where I'm going wrong?
Edit
The button is inside a tab bar controller, will that affect things?
For push you need to embed your source view controller in navigation controller.You can do this as follows:
Select your source view controller, Go to Editor and select Embed In
-> Navigation Controller.