open new screen on button click in swift - ios

I'm newbie in swift iOS. In my storyboard, I've created some dynamic fields from viewController. Have created a button from object library.
Now I need to open an new empty screen after clicking this button.
In the new screen, there'll be some input fields. I'll create those through programming or object library.
But I'm unable to open the empty screen.
#IBAction func SaveTgw(sender: AnyObject) {
print("test=====")
}
I need to include the screen opening code within SaveTgw. Any help or any descriptive tutorial link would be appreciated....

1-From Utilities open identity inspector in storyboard ID enter id "MySecondSecreen"
2-In your method add the following code :
#IBAction func SaveTgw(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let vc = storyboard.instantiateViewControllerWithIdentifier("MySecondSecreen") as! UIViewController; // MySecondSecreen the storyboard ID
self.presentViewController(vc, animated: true, completion: nil);
}

Create a SecondViewController in Main.storyboard like this -
On Scan button click , do following code
#IBAction func scanAction(sender: AnyObject) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.presentViewController(secondViewController, animated:true, completion:nil)
}
There are many answers available, just do a search -
Swift: Navigate to new ViewController using button
Swift - Change view controller using action button

Do simple way. Connect segue from firstViewController to secondViewController.Next click the segue goto show attribute inspector (top 3rd option from right side).In identifier give "your text".
write below line into your button action.
performSegueWithIdentifier("your text", sender: nil)

just use this , with code
swift 2,3x,4x
if nib exist,
let vc =
YourViewControllerName(nibName:"YourViewControllerName",bundle:nil)
self.navigationController?.pushViewController(vc,animated:true)
if no nib
let vc = YourViewControllerName()
self.navigationController?.pushViewController(vc,animated:true)

SWIFT 4
Based on #AbedalkareemOmreyh's answer:
Go to Utilities, open identity inspector and enter the ID "MySecondSecreen" in the storyboard ID.
Add the following code to your button:
#IBAction func SaveTgw(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let vc = storyboard.instantiateViewController(withIdentifier: "MySecondSecreen")
self.present(vc, animated: true, completion: nil);
}

Related

2.Page ViewController are not fullscreen?

im on my first app with swift right now. and I want to make 2 Pages.
1.Page -> Button -> 2.Page
it works but the 2.Page looks not like fullscreen ?! more like a Page to slide?
i wann a normal 2.Page like the first one. hope you know what I mean :D thank you for your help.
enter image description here
enter image description here
You just have to go to story board select the 2nd view controller and change presentation style to "Full Screen". Please refer this
If your SecondViewController is created in programmatic code without the storyboard the following code will work without problem
Solution1:
class FirstController: UIViewController {
func presentSecondController() {
// create instance of secondVC
let secondController = UIViewController(nibName: nil, bundle: nil)
// Change presentation style
secondController.modalPresentationStyle = .fullScreen
// Present secondVC
self.present(secondController, animated: true, completion: nil)
}
}
`
Solution2:
2.a If SecondViewController is created in the storyboard, you will have to add a StoryboardID to your SecondViewController like the following image:
2.b instantiates secondController instance from the storyboard by adding this code:
class FirstController: UIViewController {
func presentSecondController() {
// Create storyboard instance from name file
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// Instantiate secondVC from storyboardID
guard let secondController = storyboard.instantiateViewController(withIdentifier: "secondControllerID") as? SecondController else { return }
// Change presentation style
secondController.modalPresentationStyle = .fullScreen
// Present secondVC
self.present(secondController, animated: true, completion: nil)
}
}

No response when attempting to switch to different view controller in different storyboard

I've got a button on storyboard Alpha that leads to storyboard Beta using the following code:
#IBAction func showBeta(_ sender: Any) {
let storyboard = UIStoryboard(name: "Beta", bundle: nil)
let bvc = storyboard.instantiateViewController(withIdentifier: "BetaViewController") as! BetaViewController
self.present(bvc, animated: true, completion: nil)
}
This works without any problems.
However, I ran into the issue of not having a back button in Beta because self.present makes the new view appear modally.
I then read this post where the poster had the same problem as me. The answer suggested this code:
self.navigationController?.pushViewController(viewController: UIViewController, animated: Bool)
So I changed my code to:
#IBAction func showBeta(_ sender: Any) {
let storyboard = UIStoryboard(name: "Beta", bundle: nil)
let bvc = storyboard.instantiateViewController(withIdentifier: "BetaViewController") as! BetaViewController
self.navigationController?.pushViewController(bvc, animated: true)
}
However, now nothing happens at all when clicking the button. No errors, nothing. What's going on here?
It seems your AlphaViewController isn't embedded in a UINavigationController.
Try this piece of code:
#IBAction func showBeta(_ sender: Any) {
let storyboard = UIStoryboard(name: "Beta", bundle: nil)
let bvc = storyboard.instantiateViewController(withIdentifier: "BetaViewController") as! BetaViewController
show(bvc, sender: self)
}
This will cause your BetaViewController appear modally if your AlphaViewController isn't embedded in navigation controller and you won't get the back button. But if your AlphaViewController is embedded in navigation controller then your BetaViewController will be shown as left-to-right transition and you will get your desired back button.
To add navigation controller:
If you added your AlphaViewController from interface builder, follow this to embed it in a navigation controller: SelectAlphaViewController -> Editor -> Embed In -> Navigation Controller
Or, if you added your controller programmatically, you can check this answer.

Segue to Another Storyboard Swift

I am attempting to segue to another storyboard programmatically, but every time I've tried the view loads with a black screen with no content.
Here's the code I've been working with:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let startingView = storyboard.instantiateViewControllerWithIdentifier("HomeScreenView")
self.showViewController(startingView, sender: self)
I've created the reference on the Origin's Storyboard of the Destination Storyboard and have also tried to call:
performSegueWithIdentifier("changeover", sender: self)
Any thoughts and suggestions would be greatly appreciated.
I've haven't had issues using Storyboard References with segues. Here are the steps I've followed to get it working:
1) In the storyboard with the source view controller, drag a storyboard reference onto the canvas.
2) Set the reference to point to the correct storyboard name and view controller in the attribute inspector
3) Ctrl+drag from the source's view controller icon (in the top bar of the scene) to the storyboard reference in order to create a segue.
4) Set the segue to "Show" and give it the identifier "Test" in the attribute inspector
5) In your source view controller's code, at the appropriate location where you want it triggered, add the line:
performSegueWithIdentifier("Test", sender: self)
That should be everything needed for the segue to be called programmatically!
"MyStoryBoard" this is the storyboard name where you want to go
"StoryboardId" this is the controller id which i want to show after segue.
let storyboard = UIStoryboard(name: "Service", bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: "ProfileTimelineVC") as! ProfileTimelineVC
self.present(myVC, animated: true, completion: nil)
In AppDelegate append var window: UIWindow?
let storyboard = UIStoryboard(name: "Registration", bundle: nil)
let registrationvc = storyboard.instantiateViewController(withIdentifier:
"RegistrationViewController") as! RegistrationViewController
self.window?.rootViewController?.present(registrationvc, animated: true,
completion: nil)

Swift Xcode Button not showing next view

I'm trying to code a button that simply switches to the next view. So I embedded the first ViewController into a navigation controller then I dragged in another view controller, created a new class name ViewController2 and associated that class with the second ViewController and came the second view controller a storyboard id:"view2". Then in the Button's function I added this code:
#IBAction func newGamePressed(sender: AnyObject) {
let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as
ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}
However when i click on the button, literally nothing happens!!! what am i doing wrong???
#IBAction func newGamePressed(sender: AnyObject) {
let VC1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("view2")
let navController = UINavigationController(rootViewController: VC1)
self.presentViewController(navController, animated:true, completion: nil)
}
Just try with this code.

How to switch view controllers in swift?

I'm trying to switch from one UIViewController to another using code. Right now I have,
self.presentViewController(ResultViewController(), animated: true, completion: nil)
All this is doing is taking me to a black screen instead of the ResultViewController. Any suggestions?
With Storyboard. Create a swift file (SecondViewController.swift) for the second view controller
and in the appropriate function type this:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Without storyboard
let secondViewController = ViewController(nibNameOrNil: NibName, bundleOrNil: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
You can try this one
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ResultView") as ResultViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Make sure you set your view controller identifier.
Swift 3
To present a controller modally
let vc = self.storyboard!.instantiateWithIdentifier("SecondViewController")
self.present(vc, animate: true, completion: nil)
To show a controller
self.show(vc, sender: self)
Try this:
let vc = ViewController(nibNameOrNil: yourNibName, bundleOrNil: nil)
self.presentViewController(vc, animated: true, completion: nil)
Hope this helps.. :)
The easiest way to do this would be to create a segue by right clicking on the view you are starting on and dragging the blue line to the result view controller the hit the show segue option. Then set the identifier of the segue to "segue". Now add this code wherever you would like in your view controller:
self.performSegueWithIdentifier("segue", sender: nil)
this will trigger the segue that will go to your resultViewController
The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen. Then select push. And when you will tap the button the screen will be switched to the target screen.
Make sure you are using a UINavigationViewController.
Source: ios UINavigationController Tutorial.
Shaba's answer is a good start but requires the following so that you
can control the segue from within your view controller:
override func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool {
// your code here
// return true to execute the segue
// return false to cause the segue to gracefully fail }
Source
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = vc
Or
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: vc) //If you want to add navigation functionality to that VC

Resources