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?
I am trying to dismiss a modal and push a new view controller from the navigation controller, but the code bellow dismisses the navigation controller as well, so there is nothing to push to and the window collapses. This code is inside my routing class:
func navigateToVC() {
self.navigationController.presentingViewController.dismiss(animated: false, completion: nil)
self.navigationController.pushViewController(newVC, animated: false)
}
So is there a way to dismiss the presenting view controllers while keeping the navigation controller?
Use this to dismiss presented ViewController,
self.dismiss(animated: true)
Once you dismiss it , that's where you should push ViewController. You can't push after you dismiss your viewController because it's deallocated already.
I recommend creating a delegate and call it from your HomeViewController
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.
I want to move to another view controller without using navigation controller, segue, storyboard and animation
You can use presentViewController like this
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("YOUR_VIEWCONTROLLER_ID")
self.presentViewController(controller!, animated: false, completion: nil)
When clicking on a rightBarButton, a UIPopoverController will present.
The problem is: when clicking on the NavigationBar, this UIPopoverController does not dismiss.
Why? And is there a way to resolve it?
I have tried to search, but can't find anything about this.
Thanks in advance.
UIPopoverController seems to add the navigation bar to its passthroughViews array when it is presented. I was able to solve the problem by re-setting passthroughViews to an empty array immediately after presenting the popover.
When launching from a bar button you can simply do this
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverController setPassthroughViews:nil];
Items on your navigation bar will be automatically added to popoverViewController's passthroughViews. It happens after the popover shows up. So you need to clear passthroughViews after that.
And for iOS 8, we can get popoverController from UIViewController.popoverPresentationController, before that, we can get popoverController from UIStoryboardPopoverSegue.
In your view controller presents a view controller as popover.
var popoverController: UIPopoverController?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Before IOS8, we need to get reference of popOverController from UIStoryboardPopoverSegue
if (!self.respondsToSelector(Selector("popoverPresentationController"))) {
if let popoverController = (segue as? UIStoryboardPopoverSegue)?.popoverController {
let menuViewController = segue.destinationViewController as AIMSMenuTableViewController
menuViewController.popoverController = popoverController
}
}
}
In your view controller that is presented as popover.
var popoverController: UIPopoverController?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Set passthroughViews to nil make tapping other navigation bar button
// dismiss presenting popoverController
if (self.respondsToSelector(Selector("popoverPresentationController"))) {
self.popoverPresentationController?.passthroughViews = nil
} else {
// For iOS8-pre version, we need to pass popoverController reference from segue
self.popoverController?.passthroughViews = nil
}
}
The documentation for UIPopoverController states:
When displayed, taps outside of the
popover window cause the popover to be
dismissed automatically. To allow the
user to interact with the specified
views and not dismiss the popover, you
can assign one or more views to the
passthroughViews property. Taps inside
the popover window do not
automatically cause the popover to be
dismissed. Your view and view
controller code must handle actions
and events inside the popover
explicitly and call the
dismissPopoverAnimated: method as
needed.
The navigation bar is added as one of the passthroughViews when the popover is presented from a bar button item.
Perhaps try settings an empty array as the passthroughViews property on your popover controller.
You put this cod on any other action or After completing the selection or provide some close button in the popover and accomplish uy yhing,
[popOverControllerObj dismissPopoverAnimated:YES];
This is expected behavior as far as I can tell. The popover on the bookshelf in iBooks behaves like this. Retain a reference to the popover when you present it, and then dismiss it if any of the buttons in the navigation bar are tapped.