Xcode 11 Segues - swift

Whenever I am using segues in the Xcode 11 beta (here just changing between two VCs using a swipe gesture), the second VC pops up as a card:
How can I prevent this?

Id take a look at this article. It explains well why its happening and gives an example of how to revert it back to the standard style.
View Controller Presentation Changes

If anyone is doing the segue programmatically, the code needs to be something similar to this:
#objc func buttonClicked(_ sender: UIButton) {
let vc = ViewControllerB() //Destination controller
vc.modalPresentationStyle = .fullScreen // This line is needed now
vc.modalTransitionStyle = .flipHorizontal
self.present( vc, animated: true, completion: nil )
}

Related

Presenting ViewController just shows black screen

I am trying to present a ViewController from a Button action. But this seems to not work as i want it to. I do not get any error but after clicking the button the screen turns black and nothing happens. The only things i could manage to find are for objective-c and i am just trying to learn Swift. I am using storyboard to design this and added the Viewcontroller in the appropriate place in the designer
Code:
#IBAction func doShow(_ sender: Any)
{
var newWindowViewController = NewWindowViewController()
newWindowViewController.baseItem = self.baseItem
present(newWindowViewController, animated: false)
}
You need to instantiate the ViewController first from your storyboard. Else it won´t contain any View and turn up black.
#IBAction func doShow(_ sender: Any)
{
// Instantiate your ViewController here
var newWindowViewController = storyboard?.instantiateViewController(withIdentifier: "putyourIdentifierhere")) as! NewWindowViewController
newWindowViewController.baseItem = self.baseItem
present(newWindowViewController, animated: false)
}

How to move to other viewcontrollers by using button and textfield?

How to move to other viewcontrollers by using button and textfield?
I've got over 100 viewcontrollers.
For example
If a user put 10 in the textfield and click the button next to it, he can move to the viewcontroller with storyboard ID '10'
Or if the user put 77 in thr textfield, he can move to the viewcontroller with storyboard ID 77.
Or similar like this...
Thank you.
You should be able to accomplish this doing something like:
#IBAction func textFieldButton(_ sender: UIButton) {
let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: textfield.text!)
self.present(vc, animated: true, completion: {})
}
Summary
As long as the ID for the ViewControllers matches whatever is being typed in the textfield, that should do the trick. You might want to add additional code to verify whatever is being entered.

How can I make a 3d Touch Quick Action open a certain view controller in my app?

I have an app that has a 3D Touch quick action that I want to have it launch a certain view controller in my app.
Could someone help inform me what the correct code would be to launch a certain view controller in my app?
In my AppDelegate.swift I have this... but it's not working.
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "Matt-Held.blackshirtMarket.viewproducts" {
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination as!
productViewController
}
The view controller I want to "launch" when the 3d quick action is pressed is called "productViewController"
Thanks in advance!
You want to instantiate your target VC from the storyboard, like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let targetVC = storyboard.instantiateViewController(withIdentifier :"productViewController") as! ProductViewController
Then you want to decide what to do with it. Some common options might be a) present it modally, b) push it onto an existing Nav Controller, or c) just make it the rootVC of your app's window. Here's how you would do the latter:
// option B) push onto an existing nav controller
if let navC = window?.rootViewController as! UINavigationController? {
navC.pushViewController(targetVC, animated: false)
}
// option C) just make it the root VC
window?.rootViewController = targetVC
I think the other methods are well documented elsewhere. What you want depends on the navigation structure of your app (which you didn't include in the question).
Apart from that, do take a look at the sample shortcuts application. And remember that this method, performActionForShortcut, is called when a quick action is used to activate your app from the background. So you will also need to deal with launching your app via a shortcut.

How to programmatically show modal in a UINavigationController

In my app, I have a UINavigationController that i'm pushing and popping ViewControllers from. At some point, I want to show a VC modally (showing the previous controller "underneath"). I can get it to work by setting up a segue in a storyboard, however I'm in a spot where I need to do it programmatically, and I can't seem to find the right magic incantation to make it work.
I saw a couple of similar questions but they seemed to be showing the UINavigationController modally, not showing one of the VC's on the UINavigationController stack modally.
(I put up a test application here: https://github.com/SuperTango/ModalNavController, and that's where this code and images come from)
The "Manual" code does:
#IBAction func goToVC2Tapped(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "VC2ViewController") as! VC2ViewController
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.pushViewController(destinationViewController, animated: true)
}
but it's not working (see the second transition in the gif below).
The segue that works is setup like this:
This gif is from the test app and shows how it works with the segue, but not manually.
Any ideas? Thanks!
To present modally you need to use:
present(destinationViewController, animated: true, completion: { })
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
destinationViewController.modalTransitionStyle = .crossDissolve
self.present(destinationViewController, animated: false, completion: nil)
I think this helps show your ViewController in a modal way of presenting. Write the above code under outlet actions or where you want to present your ViewController.

push UISplitViewController upon login using swift

I am trying to push UISplitViewController upon button click but not having much luck. When I run the app I have the FirstViewController which is on the storyboard as the intial screen which only have a button, upon button click I want to push UISplitViewController, below is the code I am using upon button click, can someone please suggest what I am doing wrong, thnx
#IBAction func launchSplitView(sender: AnyObject)
{
let leftVC = LeftViewController()
let detailVC = RightViewDetailController()
let splitViewController = UISplitViewController()
//splitViewController.delegate = //UIApplication.sharedApplication().delegate as AppDelegate
splitViewController.viewControllers = [leftVC,detailVC];
//splitViewController.delegate = self
splitViewController.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
window?.rootViewController = splitViewController
// window.makeKeyAndVisible()
}
You can't use window?.rootViewController because your storyboard is the rootViewController instead you should use presentViewController
#IBAction func launchSplitView(sender: AnyObject){
let leftVC = LeftViewController()
let detailVC = RightViewDetailController()
let splitViewController = UISplitViewController()
splitViewController.viewControllers = [leftVC,detailVC];
self.presentViewController(splitViewController, animated: true, completion: nil)
}
For now this is the solution I found
=>I was able to launch through segue but not programmatically
When I programmatically launch using "launchSplitView function", I have issues with TableviewController which is the first view controller in split view but when I use segue it works. Not sure if its a bug or what. Segue is good enough for now.