I'm a swift newbie and I've got a simple question which I hope someone can help me figure out.
I have a multi-tab app. I created some segues from the tab view controllers on the Stopryboard. I've given the segues identifiers and I'm calling them from my Tab1ViewController code using performSegueWithIdentifier("tab1ToMyTarget", sender: sender) no problem.
However, I'd like to be able to call the segue from any of the app's tabs without creating new segues from the other tabs' view controllers (i.e. I don't want to create "tab2ToMyTarget" - I presume there's a better way!).
My question: Do I create these 'universal' segues on the tab bar view controller (e.g. "tabBarToTarget") (and if so how do I call it from one of my tab view controllers)? ...or...
Do I keep the segue from a single tab view controller (tab1ToTarget) and call that segue from a sibling tab view controller somehow?
First, set the view controller that you want to go to's storyboard Id. Then run this:
let vc = storyboard!.instantiateViewControllerWithIdentifier("someViewController")
self.presentViewController(vc, animated: true, completion: nil)
You cannot use the same segue to open a controller from a different one. It's better to instantiate a view controller with its storyboard id and then present/show based on the flow.
Related
A storyboard allows me to setup a nested navigation controller with a push segue (and it works fine):
But if I try and do it without a segue in code, I (expectedly) throw an error:
'Pushing a navigation controller is not supported'
#IBAction func showTapped(_ sender: Any) {
guard let secondNav = self.storyboard?.instantiateViewController(withIdentifier: "SecondNavID") else { return }
self.navigationController?.pushViewController(secondNav, animated: true)
}
Ultimately, I'm trying to reproduce the above storyboard behavior in code (without a segue), but I'm not sure what it's doing.
Is the storyboard defaulting to "present" even though it's set it to "show"?
Although a storyboard may seem to permit nested nav controllers, that configuration is illegal outside a collapsed split view controller and should never be used (according to Apple).
Here is a screen grab of both a "Show" and a "Present Modal" so this seems to be the case that the storyboard is just reverting back to a Present for a navigation controller:
https://imgur.com/a/7ytObW2
It seems as if the best solution is to remove any "Show" segues and replace with "Present"
As a workaround, you could present the navigation controller instead of pushing, therefore you will should have the hierarchy as:
Nav Controller embeds view controller => presents Nav Controller embeds view controller.
So I have a tabbed application, and I'm really new to xCode. I created a first view.
So I need help with code for the Quotes button to help lead me to the tab bar controller. Thanks
It looks like you have a programmatic ("custom") segue in the view controller. This probably isn't what you want.
In Interface Builder, select that segue and in the Attributes Inspector, change the kind of segue to "Show (e.g. Push)".
First of all, it looks like you're using a custom segue and it looks like you don't actually need it in your situation.
There is a simple solution by changing the segue Kind to Show (e.g. push). This will simply show the connected view controller on top of your first viewcontroller.
This won't allow you to navigate back to the first view controller. If you want to do so, click on the first view controller in your storyboard and go to Editor > Embed in > Navigation controller.
This will automatically add a navigation controller which will manage the default navigation for you.
click on button press and hold control and drag it to another view controller some actions will pop than select the first option show and you are ready to go.
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let tb = storyboard.instantiateViewControllerWithIdentifier("Storyboard ID") as! UITabBarController
self.presentViewController(tb, animated: true, completion: nil)
To return from TabBarController
self.dismissViewControllerAnimated(true, completion: nil)
My current setup of viewcontrollers are:
tab view > navigation controller > table view controller > navigation view controller > cell details. Please see
The current setup of viewcontrollers
I used to have:
tab view > navigation controller > table view controller > cell details
and then everything was fine.
The issue is that I need a custom action to happen when the user presses the back button, and to do this i added a nav controller between the "table view" and the "cell details". And thats when the tab bar disappeared. I understand this seems to be "normal" behaviour, but that don´t help me much. Please help.
The code that segues to the detail view controller. (I use the storyboard, so light on code for these things)
#IBAction func add(sender: AnyObject) {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("TableViewToDetailView", sender: self)
}
It happens when hideTabBarOnPush property is true(in code) or Hide Bottom Bar on Push is checked on storyboard for the controller you are pushing.
I had a similar problem and the right question was kind of hard to ask. using Tsb Bar Controllers with Navigation Controllers and View Controllers os too tricky and certain things a re not allowed and there is a lot of terminology, and, there are many different types of segues,and there are many different kinds of consequences for doing certain things.
I found the correct procedure (answer) in the second part of this two part series:
Storyboards Tutorial for iOS: Part 1
Storyboards Tutorial for iOS: Part 2
Summary of the procedure:
Embed the source and destination View Controllers in Navigation Controllers, then create unwind segues (methods with the signature#IBAction func methodname(_ segue:)) in the source view controller. Then, from the destination View Controller, control-click from the navigation bar button (or any view required to trigger a return to the first view controller) to the Exit object above the view controller then pick the correct action name from the popup menu. The unwind segue will be accessible from the Document Outline and can be given an identifier to use in prepare(for:sender:) in case data needs to be sent to the from the destination view controller. Also, the segue from the first view Controller to the second navigation controller must be modal.
A similar issue I met although maybe not quite the same as yours but hope it may help. When view controller A presents view controller B, the hidesBottomBarWhenPushed property of B could be overridden by the hidesBottomBarWhenPushed property of A. I fixed it by setting B's modal style.
I want to design a routing mechanism for management view controller transitions in objective-c.
Two view controllers, without reference to the other side of the pointer, the transition is completely controlled by the route.
How to achieve the route?
Thanks for your help.
I hope you think like this.
Route = Segue (In Xcode)
Take Two View controller
Make 1st Embed in Navigation controller (Which is must be initial View Controller)
Put One Button in 1st VC
Press Ctrl + Drag Mouse from button to 2nd VC (One popup will be show, then select Push)
Run Project
Press Button
Lol
You can use Storyboard's segues. Create view controllers and segues between them. You can use Push Segue or Model Segue. Push Segue will let you pop next view controller too, and you will have to embed your first view controller into navigation controller. For Model Segue, you can move from one view controller to another but to return to previous view controller, you will have to create another segue.
I think I find the approach to answer this question。 Here is the similar project.
My app has a menu and a UITabBarController. What I want to do is to display a view controller that belongs to my menu but not to the UITabBarController, However I don't want to remove the UITabBarController. I tried codes similar to the one below but they are removing the UITabBarController.
tabBarViewController.selectedViewController?.presentViewController(ExtraViewController, animated: true, completion: nil)
You should get UINavigationController of selected ViewController and then push view you want to present. Otherwise you are presenting modal view controller with presentViewController witch hides your UITabBarController view.
Im not at my computer right now, and cant post any code, but hope this helps.
Best way to do it is to use UINavigationController. You can create new one programmatically and put your menu controller as root.
And if you put this UINavigationController as one of views in UITabBarController then you can preform a code like:
[self.navigationController pushViewController:ExtraViewController animated:NO];
Also you can use storyboard to create your controllers hierarchy like that:
To do that select your menu controller and go to Xcode menu>Editor>Embed in>Navigation Controller and then Xcode menu>Editor>Embed in>Tab Bar Controller.