how to passdata between storyboard - ios

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
if UIDevice.current.orientation.isPortrait{
let secondView: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let main = secondView.instantiateViewController(withIdentifier: "ViewController") as! ViewController
main.modalPresentationStyle = .fullScreen
self.present(main, animated: true, completion: nil)
}
if UIDevice.current.orientation.isLandscape{
self.dismiss(animated: true, completion: nil)
}
}
I use this code to switch between two different storyboard's viewcontroller. How can I pass data between those viewcontroller in two different storyboard.
I have tried "main.instanceData = myData before the present" It seems could work, but the initial value of myData is nil, so when this line of code was executed, it would show "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"

Just try this. You can pass data to viewController directively like that:
let secondView: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let main = secondView.instantiateViewController(withIdentifier: "ViewController") as! ViewController
main.data = yourdata
main.modalPresentationStyle = .fullScreen
self.present(main, animated: true, completion: nil)

Related

Cannot display View (Programmatically)

I'm learning how to make an app with just a code in Swift. I have encountered this problem:
This is action of a button.
#objc func answerAction() {
let story = UIStoryboard(name: "Main", bundle: nil)
let controller = story.instantiateViewController(withIdentifier: "AccountViewController") as! AccountViewController
self.present(controller, animated: true, completion: nil)
}
If I press it, it shows this error:
Thread 1: "Could not find a storyboard named 'Main' in bundle NSBundle </Users/mas/Library/Developer/CoreSimulator/Devices/A3BEC6D0-3AA6-4193-A755-1181DD580576/data/Containers/Bundle/Application/C80D5D36-EBD8-44D4-AF14-B64E2E7E5587/AppForTest.app> (loaded)"
As I understand, the problem is that I have deleted Main.storyboard and app cannot reach it. So how I should declare in a answerAction story constant?
#objc func answerAction() {
let controller = AccountViewController()
self.present(controller, animated: true, completion: nil)
}

how to present and dismiss at same time

This is my Code
#IBAction func backButton(_ sender: UIButton) {
self.dismiss(animated: true, completion: { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
self.present(vc, animated: true, completion: nil)
})
}
this is not working only dissmising
I dont't understand your questions well. I think you want to present a new ViewController. You can use this:
UIView.animate{duration: 0.5, animations: {
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = NextViewController()
}}
Problem is that you are dismissing self and then trying to present a new view controller on that exact same self - but at that moment self is already dismissed. You need to present that new view controller from the parent of the current view controller. I believe the best way is to setup a delegate. So the controller that you showed us and that you want to dismiss will have this:
protocol FirstDelegate: class {
func firstDismiss(_ first: First)
}
class First: UIViewController {
weak var delegate: FirstDelegate?
#IBAction func backButton(_ sender: UIButton) {
// this will tell the delegate to dismiss this contorller and present the other one
delegate?.firstDismiss(self)
}
// rest of the code omitted
}
Next, you will have to setup this in the parent that presents First view controller (here I assume that presentFirst method presents the First view controller):
class ParentViewController: UIViewController, FirstDelegate {
// rest of the code
func presentFirst() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstVC = storyboard.instantiateViewController(withIdentifier: "First") as! RequestItemPackageDetails
// set delegate to first:
firstVC.delegate = self
self.present(firstVC, animated: true, completion: nil)
}
func firstDismiss(_ first: First) {
first.dismiss(animated: true, completion: { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
self.present(vc, animated: true, completion: nil)
})
}
}
Not quite sure with your Qns, but try this one out.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
vc.previousVC = self
self.present(vc, animated: true)
RequestItemPackageDetails.swift
var previousVC : UIViewController!
override viewDidLoad() {
previousVC.dismiss(animated: false, completion: nil)
}
You can dismiss self and then present another view from rootViewController
self.dismiss(animated: true, completion: { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController?.present(vc, animated: true, completion: nil)
})

How do I force a ViewController to present without the user pressing any buttons?

When a function is called, I would like SixthViewController to be presented to the user without the user needing to press any buttons. I know how to present another ViewControllerby setting a button to "Show" another ViewController, but how can I programmatically force SixthViewController to present itself once the function is called?
you can present viewcontroller with this way:
let destVC = SixthViewController()
self.present(destVC, animated: true, completion: nil)
or
let storyboard: UIStoryboard = UIStoryboard(name: "storyboardNamehere", bundle: nil)
let destVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifierhere" as! SixthViewController
self.present(destVC, animated: true, completion: nil)
========================================================================
just write down this function and also called it when u want to push or write down it in any function like func pushViewController and called it self.pushViewController() that's it bro.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let sixthViewController = storyBoard.instantiateViewController(withIdentifier: "SixthViewController") as! SixthViewController
self.navigationController?.pushViewController(sixthViewController, animated: true)
If you are not using segues but navigation controller try this:
let destinationVC = SixthViewController()
self.navigationController?.pushViewController(destinationVC, animated: true)
If you just want to show it without navigation controller:
let destinationVC = SixthViewController()
self.present(destinationVC, animated: true, completion: nil)
If you are using storyboard:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destVC = self.storyboard?.instantiateViewController(withIdentifier: "SixthViewController" as! SixthViewController
self.present(destVC, animated: true, completion: nil)

How to get Current viewcontroller (currently Presenting) using storyboard id in swift programmatically?

**How to get the presenting view controller in swift. I am using storyboard id.**And for presenting next viewcontroller how can I add to the hierarchy?
I tried like this:
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let logincontroller = mainStoryboard.instantiateViewControllerWithIdentifier("LoginViewController")
if UIApplication.sharedApplication().keyWindow?.rootViewController.presentingViewController == logincontroller {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nextVC = mainStoryboard.instantiateViewControllerWithIdentifier(storayboardIdentifier)
self.presentViewController(nextVC, animated: true, completion: nil)
}
In Appdelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if isLoggedin == false {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
else {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
}
The code works, it display the view but it still have a warning
Failed to instantiate the default view controller for
UIMainStoryboardFile 'Main' - perhaps the designated entry point is
not set?
Check initial entry points.
Also to present screen below code practice is better way.
let objNextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ID_NextViewController") as? ViewController
self.presentViewController(objNextViewController, animated: true) { () -> Void in
print("Achive")
}
EDITED :
If you want to present viewcontroller from already presented view controller then use below code.
var currentViewController : UIViewController!
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
if let viewControllers = appDelegate.window?.rootViewController?.presentedViewController
{
self.currentViewController = viewControllers as UIViewController
}
else if let viewControllers = appDelegate.window?.rootViewController?.childViewControllers
{
self.currentViewController = viewControllers.last! as UIViewController
}
let objNewViewController : NewViewController!
self.currentViewController.presentViewController(objNewViewController!, animated: true, completion: { () -> Void in
NSLog("Achived")
})
try this code for presentingViewControllerusing storyboard name:
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
self.presentViewController(viewController, animated: false, completion: nil)
try this code,
let storyboard : UIStoryboard = UIStoryboard(name:"Main", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
hope its helpful
My guess is that you haven't set an initial view controller for your storyboard.
To fix this open your storyboard and select the UIViewController you want to display as the first one. Then in the Attributes inspector tab(third one from the right in the utilities panel), look under the View Controller section and make sure the Is Initial View Controller checkbox is checked.

Get Instance Of ViewController From AppDelegate In Swift

I am trying to load a specific ViewController from the app delegate in swift when a user clicks a UILocalNotification. I have figured out that this is called in this function:
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!)
But when I try and access one of the open ViewControllers I think it's returning null because my application is crashing. Here is what I am trying:
var rootViewController = self.window!.rootViewController
var storyBoard = rootViewController.storyboard
var setViewController = storyBoard.instantiateViewControllerWithIdentifier("CurrentShows") as ViewController_CurrentShows
rootViewController.navigationController.popToViewController(setViewController, animated: false)
setViewController.reloadData()
It's crashing on the popToViewController line.
You could try:
let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CurrentShows") as! DetailViewController
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)
Swift 3:
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = mainStoryboard.instantiateViewController(withIdentifier: "viewController")
self.present(viewController, animated: true, completion: nil)

Resources