I have a storyboard with a View Controller has my initial view. I am trying to segue to a NavigationController from a button click.
I can make the segue work when the button is clicked, but how do I get a reference to my NavigationController so I can populate my TableView?
Also, how do I make the table view go back to the initial view?
My Storyboard looks like this:
Thanks for the help.
First, in general we don't segue to nav controller, we segue with them, to another view controller. In theory, your segue will only work (unless you have done a lot of unnecessary coding) if you have properly setup the storyboard to make the Navigation controller the "initial" view controller, and your first View Controller as it's "root view controller" (most easily accomplished by the menu's "Embed in... > Navigation Controller" command).
Then in your code, you always have a simple access point for your nav controller: Any view controller which is currently in the stack of controllers managed by a nav controller can simply use it's navigationController property to access it. You can then use it's interface to popViewControllerAnimated: or similar.
In your View Controller, implement
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
and get your NavigationController via segue.destinationViewController.
But if you want to go back, you probably want the ViewController with the button to be connected to the NavigationController, not the other way around.
NC => VC with button -> TableVC
where => is a relationship segue and -> is a push segue.
Related
I am presenting a modal view controller with a tableView, and then want to perform a push segue when a cell is tapped inside the modally presented view controller.
I set up a view controller and a show (push) segue from the modal view controller to the new view controller, but when I call performSegueWithIdentifier, the new view is presented in a modal fashion (slides up) on top of the initial modal view, instead of coming in from the right.
Anyone know what's going on?
Thanks!
For anyone who has this issue in the future, I figured it out:
You need another navigation controller. So I perform a modal segue to a new navigation controller, and set the modal view controller as the root view controller of the navigation controller. I can then perform a show segue from the modal view controller to any other as normal.
:)
As of Xcode 11.4 (possible earlier) you can also just select the destination view controller and in the right inspector pane under the attribute inspector you have the option: "Transition style" and "Presentation" which will allow you to change from Automatic to Full Screen for example. That way you can overwrite that when you use "push" from a modally presented view controller, the segue will always present the next view controller modally.
I have a tableview with about 10 items.
I have set up a UIView Controller as a detail page.
I Control-Drag from my cell in the tableview to the new view controller, and I can create a Push Segue, however the navigation control does not appear on the new view controller.
When I run the application my prepareForSegue method executes but my application does nothing. The detail UIView Controller does not display.
What step am I missing to make this work?
The Push segue only makes sense for ViewControllers embedded inside a UINavigationController. You can do this easily by selecting your tableview's ViewController in the storyboard and choosing in the main menu:
Editor -> Embed In -> Navigation Controller
I have two UIViewControllers, vc1 and vc2.
I want to switch between them. But before loading the view of the new view controller, I want to destroy/release/remove (I'm not sure abt the correct word to use here) the previous viewcontroller.
For example, when I am switching to vc2 from vc1 ,I want to destroy vc1 completely, so that when I return to vc1 from vc2, vc1 will be loaded from scratch (i.e. viewDidLoad will be executed).
Which type of view switching should I opt for?
presentModal...
addSubview.
I am not using a navigation controller.
Currently I am using the presentModal... method, but when I use dismissModalViewcontroller on the newly presented view controller, it doesn't show up a new instance of the previous view controller. Instead, it shows the already running instance of it.
I want the viewDidLoad method of the previous view controller to run when I dismiss the newly presented view controller.
What exactly needs to happen in viewDidLoad?
You also have viewWillAppear available to you, so it could be that you could move the required functionality there and still use the modal presentation.
See this answer. You can do this with or without animation.
Animate change of view controllers without using navigation controller stack, subviews or modal controllers?
my app first viewController is UIViewController.
and when user click button firstView disappear and push UITabViewController
is it possible?
i can't find how to push UITabViewController from UIViewController.
UPDATE sorry, I misread TabVC for UITableViewController. Do you mean UITableViewController or UITabBarController? I'll leave my answer below anyways.
In this instance, it's usually best to have a UITabBarController be the root view object. Although it can be done, it's a messier implementation, in my opinion.
I would in fact make the UITabBarController the root and display the UIViewController modally from that UITabBarController on launch.
The user would be presented with the UIViewController and when they clicked the button, dismiss that modal view, revealing the UITabBarController.
Just use a UINavigationController.
Use the navigation controller to push the tableView as the second level in the hierarchy. As a bonus you'll get the back button for 'free' and you don't have to worry about delegates for getting back to the original UIViewController.
you may try this:
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:2];
it should work because selectedViewController property contains view of selected tab.
First of all you have view controller . And make Second view controller which contain tabbarcontroller . Now just push second view controller . And add tabbarcontroller's view as a subview to second view controller .
Hope you gets it ..
I have a Navigation Controller with a root table view which has several links. Tapping each link moves to the next view (by pushing it to the navigation controller's stack). But suppose that in that "next view", I have a UIButton that should take me further to another view (by pushing on to the same navigation controller's stack)...
View Controller-->first view-->second view-->third view..........
Now, I can easily access the Navigation Controller when I deal with the first view (and successfully push it to the Navigation Controller's stack) because it has been instantiated in the same file itself. What my real doubt is--How do you access a Navigation Controller in a far off view controller (eg, the third view or fourth view etc)? Please note that I am not using any separate delegate. All the Navigation Bar methods have been implemented in one file and connected to the Navigation Controller via an outlet.
When you push a ViewController onto a NavigationController the ViewController will automatically have it's navigationController property set. This means you can access the same NAvigationController no matter where you are in the stack.
-Update-
navigationController
In every UIViewController you can access that property.
So to in any other UIViewController that has been pushed onto the stack you should be able to just do this:
[self.navigationController pushViewController:othercontroller animated:YES];
Look at the documentation for UIViewController to see what other magic properties you have available.