How to navigate from popover view controller in swift? - swift

I have a button on a view controller and by clicking that button a view controller appears as a popup. That popup has a tableview and whenever I click on the tableview row I want it to navigate to different view controller. But I think it is not as simple as using this code.
let vc = storyboard?instantiateViewController(identifier: "controller") as! Controller
self.navigationController.pushViewController(popupView, animated: true, completion: nil)
Is there a way to open a view controller from popup view?

Related

Switch tab bar controller view controller after dismissing a modally presented view controller

In my project you can create a post from a modal view.
When the modal view is dismissed (user presses on save post) I want to switch the tab bar controller to the second tab (post feed screen).
This topic is similar to my problem. The only difference being this is presented from a modal view. I can't figure out how to implement it in my code (tab bar is nil)
Switch tab bar programmatically in Swift
I have added 3 images to make this issue clearer
code screenshot
console message
#objc func saveAction(sender: UIButton) {
print ("> save pressed")
print(presentingViewController?.tabBarController)
print(presentingViewController)
presentingViewController?.tabBarController?.selectedIndex = 1
dismiss(animated: true)
}
edit: sorry stack overflow doesn't allow me to add images yet
You can do this using delegate pattern. But if you prefer not to add a delegate for this, you can do as shown below;
You can switch the tabbar by changing the selectedIndex property of tabBarController
if let presenter = presentingViewController as? LibraryViewController {
presenter.tabBarController?.selectedIndex = 1
}
dismiss(animated: true)
If you are presenting the modal on navigation controller in tabbar, use:
if let tabBar = presentingViewController as? UITabBarController {
tabBar.selectedIndex = 1
}
dismiss(animated: true)

How to display the tabs when click the back button from hided tab view controller

I created a tab bar controller and from one tab item I gave segue to the navigation view controller. And I create a some view controllers attached to navigation controller. So in one view controller I don't need a tabs so in that controller I wrote to hide the tab bar controller that is self.tabBarController?.tabBar.isHidden = true.
When I click the back button of navigation controller from hided tab view controller to previous controller, it doesn't show the tab bar items in previous controllers. But I needed tabs in all view controller except in one view controller. Why does it not show the tabs?
This is my story board :
You can try this in the VC that's before the one you hide the tab in
override func viewWillAppear(_ animated:Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
You can use hidesBottomBarWhenPushedin the view controller which you don't need tabs. Fits your situation.
let controller = ViewControllerTwo()
controller.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(controller, animated: true)
A little more explanation:
self.tabBarController?.tabBar.isHidden = true globally changed the self.tabBarController's property hideTabBar across its children controllers stack.

macOS - Transition between view controllers like iOS, dont open separate window

Im building an app in swift for macOS, and I have a button on my initial view controller that I want to display a different view controller. I ctrl dragged from that button to the new view controller, and all of the segue options display the new view controller as a new window, rather that replacing the initial view. How can I make this button transition view controllers similar to how it works in iOS apps?
You can first make your segue to the 2nd view controller and then create an IBAction from your button where you call
self.dismiss(animated: true, completion: {})
within it. By doing this your initial view controller will no longer exist.
More detailed:
Segue from your button to the second view controller (ctrl drag)
Make an IBAction by ctrl drag from your button to the view controllers code
Put this into your IBAction:
self.dismiss(animated: true, completion: {})
Now it should look something like this:
#IBAction func dismissView(_ sender: Any) {
self.dismiss(animated: true, completion: {})
}
By clicking on your button your application go to the next view controller and the initial view controller will be dismissed.

Segue To Navigation Controller in Tab Bar Controller

In my application, I have a default tab selected for my Tab Bar Controller. On the viewDidLoad() of the default tab, I have this:
if (NSUserDefaults.standardUserDefaults().stringForKey("defaultCode") == nil) {
//let navController:UINavigationController = UINavigationController()
//self.presentViewController(navController, animated: true, completion: nil)
self.tabBarController!.selectedIndex = 1
I thought calling selectedIndex would do what I was looking forever; however, it only changes the selected tab in the TabBar, but my view does not change. The area I commented out quickly shows the proper view, then goes to a black screen. The view I am trying to switch to is a Navigation Controller
selectedIndex always be the first and pop/push after selected

Issue dismissing Popover presenting another ViewController

I've create a Popover segue in my project and inside a button to when it's touched open another ViewController modally. Then inside the another ViewController I've a button to dismiss the actual ViewController and the Popover too with the following code :
var tmpController :UIViewController! = self.presentingViewController;
self.dismissViewControllerAnimated(true, completion: {()->Void in
println("done");
tmpController.dismissViewControllerAnimated(true, completion: nil);
});
My problem is that when the actual ViewController is dismissed the Popover is like in FullScreen, then it resize to its original size and is dismissed after, and the resize process is animated.
What is causing this behaviour ?
How can I avoid this?
I would suggest you to dismiss the popover view controller from the parent view controller using delegation/notification.