I have three view controllers:
messagesVC: doesn't have navigation controller a chatting room
viewControllerA: has navigation controller and directs to messagesVC
viewControllerB: has navigation controller and directs to messagesVC
All three view controllers have their own storyboard for organization purposes. Right now, I created a storyboard segue to move from vcA or vcB to messagesVC.
I want to be able to put messagesVC on either vcA or vcB each time it's shown so that push view controller animation would work instead of presenting messagesVC modally. How would I achieve this??
In situation when you have segue in storyboard, choose the type of it 'show'.
When your messageVC is in another storyboard, you should load it like this:
let storyboard = UIStoryboard(name: "VCAStoryboard", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("messageVC") as! MessageVC
Then push it like this:
nvaigationController!.pushViewController(vc, animated: true)
This will do the job.
Related
I am working with UISplitViewController. I have UISplitViewController which contain UITabBarController. So I want to display tab bar in all the screen through out the application (Even in detail screen of UISplitViewController for iPhone). Please see the approach:
use segue to move from one controller to another controller or move via navigation so in the next controller your tabbar will be show
just like if u want to move from A to B Controller set stroyboard id ie "SecondVc"
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondVc")
self.navigationController?.pushViewController(vc, animated: true)
or just make connection using storyboard
I'm trying to trigger a modal view from the AppDelegate every time the app is opened. I can see my breakpoint being hit, but the modal never shows. I'm including an image of my storyboard in case it matters. It's a fairly simple app right now with a 2 tab tab bar controller.
This is the code I have in the AppDelegate to trigger it.
let newVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginView")
let view = window?.rootViewController as! UITabBarController
view.selectedViewController?.show(newVC, sender: nil)
It seems newVC is not in the tab bar's controllers array, and you're attempting to modally present it from the selectedViewController in AppDelegate where there probably isn't a selected view controller yet.
One solution is to present newVC after viewDidLoad of the selected view controller (view controller at selectedIndex). If the presentation must take place before any of the tab bar's view controllers are loaded, then you might wanna set it as the window's root view controller and set the root to be the tab bar once newVC has finished its business.
I have a problem with changing root in my app.
Design of my app.
After I login into app I would like to change root vc to UITabBarViewController to clean a stack.
I've faced multiple problems.
Setting vc to tab bar on apply login action -> or in bottom vc:
self.performSegue(withIdentifier: "goToMainTabBar", sender: nil)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? TabBarViewController {
UIApplication.shared.keyWindow?.rootViewController = vc
}
}
The app going to crash with:
Application tried to present modal view controller on itself. Presenting controller is TabBarViewController
Next problem is if we set a root in TabBarViewController viewDidLoaded.
UIApplication.shared.keyWindow?.rootViewController = self
Tab bar items embaded in UINavigationController doesnt have Navigation controller in it selfs, so the nav vc is not instantiated ? Becouse wheenver I will enter into item vc child -> I can't back any more.
If i won;t change a root vc there everything is fine.
For 1) you can't present a view controller using a segue and then use it to replace the root view controller in the prepare. You will need to instantiate the tab view controller from the storyboard and then replace the root view controller.
Something like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "TabController")
UIApplication.shared.keyWindow?.rootViewController = vc
(assuming the storyboard is called 'Main' and you give the tab controller the storyboard ID of 'TabController'.
I'm not quite clear on what the issue is for 2.
However as a general note I would approach this differently and instead of having the login controller as your initial view controller have the tab bar as the initial controller and then just present the login controller the first time the app starts. That way you avoid replacing the root controller at all and it's all more controlled.
I have navigationController embedded VC(Viewcontroller) in storyboard 1 which is connected to storyboard reference of storyboard 2.
Now, I have VC2 which is again NavController Embedded in storyboard 2.
I am performing the following code :
let storyboard = UIStoryboard(name: "Settings", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "setEdit") as? EditProfile
navigationController?.pushViewController(vc!, animated: true)
settings is storyboard 2, setEdit is ID from navigationController of the destination VC.
When I execute this code, It doesnt perform the presentation of new controller. Also, I have a custom segue class that transitions VCs from Right to Left.
when I use :
present(vc!, animated: true, completion: nil)
It just pushes the VC from bottom to top.
Now I am totally out of Ideas.
My query is:
How can I exactly implement custom segue from one storyboard to another, having navigation bar at the top.
When you use UINavigationController the transition is showed as you said from right to left (push), when you present a view controller modally the presentation style and the transition style are different. Now you cannot connect two navigation controllers, so I suggest to connect directly the controller in the settings storyboard without embedding it in another navigation controller. In settings storyboard you have to set your EditProfileVC as initial view controller and check its identifier and then you can push it from your first storyboard.
hello I want to have the push segue default animation while Presenting View Controller.
I have three controllers In my storyBoard. FirstController is attached to the navigationController. first controller and second is connected through segue as present Modally. I am launching the third controller from second controller like this
let takeProductPhotoController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("takeProductPhotoController") as! TakeProductPhotoController
let nav = UINavigationController(rootViewController: takeProductPhotoController )
self.presentViewController(nav, animated:true, completion:nil)
The problem is ThirdController is popping up like modally I mean from the bottom which I don't want. I want to have a animation like push segue
I think It will work If I do self.pushViewController but pushViewController is not in this self
You need to do:
self.navigationController?.pushViewController(nav, animated:true)