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

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.

Related

Xcode 11 Segues

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 )
}

How I can put back button in first scene in Swift3.0?

I have two storyboard, SB_A,SB_B
So when I click the button at SB_A, it call
let storyboard: UIStoryboard = UIStoryboard(name: "SB_B", bundle: nil)
let nextView = storyboard.instantiateInitialViewController()
self.present(nextView!, animated: true, completion: nil)
So I can see SB_B storyboard.
But I can't see the back button at first scene in SB_B
I want put back button at right scene and when I click that, back to SB_A storyboard.
How I can put back button at first scene in storyboard?
add these lines in SB_B viewdidload
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(self.leftNavigationItemAction(_:)))
}
#objc func leftNavigationItemAction(_ sender: UIBarButtonItem) -> Void {
_ = popOrDismissViewController(true)
}
try this one...
No you can't use popviewonctroller or Dissmisviewcontroller to go back to previous storyboard's view controller you have to explicitly manage this back functionality to get required functionality using push or present.

How can I add a segue identifier to a programmatically modal transition?

I have a controller which is in the Main storyboard. When I click on a button, I call the displayBorneDetailsAction() action, it presents a modal view from another storyboard.
I would add a Segue identifier when I present my modal to pass data from my main view controller to my modal view controller (with prepareForSegue), but I don't know how to do it.
I tried to use performSegue(withIdentifier:), but it doesn't present the modal in the same way.
#IBAction func displayBorneDetailsAction(_ sender: Any) {
// open the modal of a borne
let storyboard : UIStoryboard = UIStoryboard(name: "Borne", bundle: nil)
let vc: BorneVC = storyboard.instantiateViewController(withIdentifier: "BorneVC") as! BorneVC
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
navigationController.edgesForExtendedLayout = []
self.present(navigationController, animated: true, completion: nil)
}
You cannot add an identifier to a programmatical segue, since you are able to access the instance of the controller that is being presented. Simply do anything you want with the controller in the function where you present it.

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.

Presenting a a ViewController on a xib file in SWIFT

I have a xib file which I have created a pop up controller on a UIView. This pop up controller animates up when a button is pressed on a View Controller 1. I then have a button on the UIView which when pressed I want to present another View Controller (View Controller 2). code looks like:
class PopUpViewControllerSwift : UIViewController {
#IBAction func goToVC2(sender: UIButton) {
self.removeAnimate()
let VC2: VC2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as VC2
var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
VC2.modalTransitionStyle = modalStyle
presentViewController(VC2, animated: true, completion: nil)
}
}
Although thou when the button is pressed it crashes, no error or callbacks or anything. Obviously this would normally work if it was just a regular View Controller but because I am doing it inside a pop Up View which has been animated on top of another View I think that is the problem?
Can anybody help?
thanks
I did this in a UICollectionView didSelectItemAtIndexPath method.
// Register Nib
let newViewController = NewViewController(nibName: "NewViewController", bundle: nil)
// Present View "Modally"
self.present(newViewController, animated: true, completion: nil)
I set the ID of the ViewController to the same as the file name so I would be sure to reference the right ViewController.
Make sure self.storyboard is not nil.
Also confirm that VC2 is one of the ViewController's identity in the storyboard file.
I hope it could help you.
if VC2 is in a storyboard file, try to use following command to get VC2
let storyboard = UIStoryboard(name: "YourStoryboardName(maybe 'Main')", bundle: nil)
let vc2 = storyboard.instantiateViewControllerWithIdentifier("VC2") as VC2
or if VC2 is in a nib file, try to use following command to get VC2
let vc2 = VC2(nibName: "YourNibName", bundle: nil)
It should be like this:
let vc = MyViewController(nibName: "MyViewController", bundle: nil)
present(vc, animated: true, completion: nil)