perform segue as full view from another view - ios

I am trying to view another view from popover view controller as full view
#IBAction func AddCommentBTN(_ sender: Any) {
performSegue(withIdentifier: "comment", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "comment" {
let destination = segue.destination as! CommentViewController
destination.comments = self.comments
destination.myId = self.myId
}
}
when I open it it loads the view as popover view, how to open it as full view

Looks like you are using StoryBoards , here's how you do it
1)Go to the main.storyboard File and select the segue and got to its attributes inspector menu (select segue and press option + command + 5)
2)change the presentation option to fullScreen
And there you go you have fullScreen now , let me know how this went for you

Related

prepare segue - destination View Controller is called twice

In Xcode 13.1 I programmed the following code into my source view controller. It's working so well that it's calling the destination view controller twice (instead of once).
This is my code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToNextViewControllerSegue" {
let destVC = segue.destination as! DestinationViewController
destVC.categoryTag = tag
}
}
Any idea what I'm doing wrong?
Let me include the IBAction code as well (This doesn't seem to be the problem, though.)
#IBAction func startButtonTapped(_ sender: UIButton) {
if tag == 1 || tag == 2 {
self.performSegue(withIdentifier: "goToNextViewControllerSegue", sender: nil)
} else {
createAlert()
}
}
Thanks for any help!
Add a breakpoint to prepare(for segue: sender:) and look at the backtrace to see where it is being called from. My guess is that you have connected the button to the segue in the storyboard, and then also to the action method mentioned in your question, so the segue is being performed twice.

View controller with 2 destinations

I have 2 buttons in a view controller. If button 1 is clicked, then go to WebView controller. If button 2 is clicked then go to ArtView controller.
How to make override func prepare(for segue: UIStoryboardSegue, sender: Any?) have 2 destinations depending on the button clicked?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? WebViewController {
// your code
}
if let destination = segue.destination as? ArtViewController {
// your code
}
}
Hope it will help! :)
Have each button invoke a different segue, either by control-dragging segues directly from the button, or by calling performSegue(withIdentifier:sender:) from your buttons' IBAction code.
Then use code in prepare(for:sender:) to figure out which destination is being invoked.
You can:
Check the identifier on the segue (not recommended - fragile)
Check the class of the destination view controller (better than 1)
Have each view controller implement a protocol and check the protocol of the destination:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.destination {
case let webView as WebViewProtocol:
//Your code for a web view
case let artView as ArtViewProtocol:
//Your code for an Art View
}
}
Option 3 is reliable while providing loose coupling (each case can trigger any view controller that conforms to the desired protocol. All it needs to know is that the destination understands the desired protocol.)

Programmatic embed segue

I'm using a Page View Controller to implement an image carousel in my app. I have a container view in my parent view controller which has an embed segue to the Page View Controller. The problem I'm having is when I need to go off and fetch these images asynchronously and the segue happens before viewDidLoad gets the images.
My prepare for segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "startCarousel" {
let carouselPageViewController = segue.destination as! CarouselPageViewController
carouselPageViewController.images = carouselImages
}
}
in viewDidLoad:
GalleryManager.sharedInstance.getData = {
(data) in
// Assign images to instance property here ...
}
My thinking is that once I get the images that I programmatically start the segue myself. Or if I should be doing this a different way any advice would be much appreciated.
You can do something like, return false in shouldPerformSegue and initiate segue when data is loaded
i.e.
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "startCarousel" {
return (detail != nil)
}
return true
}
Then in viewDidLoad
GalleryManager.sharedInstance.getData = { [weak self]
(data) in
self?.performSegue(withIdentifier: "startCarousel", sender: nil)
}
No, you can't delay an embed segue. If you use an embed segue it fires as soon as the view controller is loaded.
You need to wait until the data loads and then call the setViewControllers(direction:animated:completion) to install the view controllers that you will use to display the data once the data is loaded.
Swift 3, Xcode 8
You can realize in this way:
set a reference to your page view controller in prepareForSegue in your masterViewController:
var myContainerViewController: UIPageViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "embedSegue" {
if let destination = segue.destination as? YourPageViewControllerClass {
self.myContainerViewController = destination
}
}
Define a function in your own pageViewController class:
func someFunction(){
//configure your image or swipe page
}
when you get the images, in your masterViewController call:
self.myContainerViewController?.someFunction()
you can't delay an embed segue.
you try to put button in view controller and then after view load set button click to set image...

UISplitViewController return from master to detail

Is it possible on iPhone via UISplitViewController to return from the master controller to the detail one? I have tried in master changing displayMode to PrimaryHidden, using showDetailViewController with splitViewController?.viewControllers[0], but nothing was able to show back the detail controller.
I would like to have both controllers without dismissing and reloading, because one includes a map and the second one preferences of map objects.
Lets say you have a button in the master view which when pressed will take you to detail view. Here is an example how that would go
#IBAction func buttonToGoToDetailView(sender: UIButton) {
performSegueWithIdentifier("segue id in storyboard", sender: nil)
}
// prpare for segua to detail view
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as? UIViewController
if let dvc = destination as? NameOfTheDetailViewController{
if segue.identifier = "segue id in storyboard" {
//use dvc to pass data to the detail if there is any
}
}
}

Push doesn't work after back button?

I'm working on an app that scans a barcode and segues into a new view controller after a scan is found. However, if the user clicks the back button (navigation controller, root view controller is a main menu, but is [probably] irrelevant), the view controller will not segue again. So it segues forward to view controller 3 the first time the app runs, but upon hitting "back" and returning to VC 2, it won't segue to VC 3 again. Is there something I'm missing? I do set the handler in viewDidLoad:
self.barcodesHandler = { barcodes in
if(self.handler_flag) {
var barcode = barcodes[0]
println("Barcode found: type=" + barcode.type + " value=" + barcode.stringValue)
self.bcode = barcode.stringValue
self.handler_flag=false
self.performSegueWithIdentifier("push", sender: nil)
}
}
And the segue:
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! SecondViewController
destinationVC.setText = bcode
}
Thanks so much!
self.performSegueWithIdentifier("push", sender: nil)
Instead of nil use self

Resources