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)
}
Related
I'm trying to present a new VC, but when I hit the code application get stuck and I can't do anything. It's quite a weird issue, how can I get out of it? My code for presenting new VC is this,
let vc : PropertyDetailController! =
UIStoryboard.viewController(identifier: "PropertyDetailController", storyBoard: "Explore") as? PropertyDetailController
vc.propertyDetailData = property
vc.hidesBottomBarWhenPushed = true
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true, completion: nil)
This is what it shows in console,
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
It isn't present new VC. This is how it my apps stuck please check gif link,
enter link description here
As I can see in your code you are moving to the new storyboard so it's necessary to move it on through the UINavigationController
Blow one is the sample code that might help you
let storyBoard: UIStoryboard = UIStoryboard(name: "YOUR_STORYBOARD", bundle: nil)
let aVC: YOUR_VC = storyBoard.instantiateViewController(withIdentifier: "YOUR_VC_ID") as! YOUR_VC
let navigationController = UINavigationController(rootViewController: aVC)
self.present(navigationController, animated: true, completion: nil)
if you want to present a view controller, try
self.present(vc, animated: true, completion: nil)
if you want to push a view controller, try
self.navigationController?.pushViewController(vc, animated: true)
Try moving the push code to main thread,
DispatchQueue.main.async {[weak self] in
self?.navigationController?.pushViewController(vc, animated: true)
}
Either present or push, all the UI handling must be done on the main thread.
Edit:
If you want to present vc, use
DispatchQueue.main.async {[weak self] in
self?.present(vc, animated: true, completion: nil)
}
First make sure current view controller embedded with Navigation Controller.
Now use below code to fix this issue.
let storyboard = UIStoryboard(name: "Explore", bundle: nil)
guard let vc = storyboard.instantiateViewController(withIdentifier: "PropertyDetailController") as? PropertyDetailController else {return}
vc.hidesBottomBarWhenPushed = true
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true, completion: nil)
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)
})
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)
I have that error when I want open new view and I don't know what does it mean.
I run that code:
func openMenu(){
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("MenuViewController") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
And the error is:
Warning: Attempt to present on whose view is not in the window hierarchy!
I have that Swift code:Swift
And I want execute with Objective-C the Swift code.
Objective-C
replace the line:
self.presentViewController(vc, animated: true, completion: nil)
with:
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(vc, animated: true, completion: nil)
})
See if it works!
or
why don't you create a segue and call it using:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Load View") {
// pass data to next view
}
}
I am new in Cocoa and Swift, and I'm trying to use the UIModalTransitionStyle.FlipHorizontal transition to switch from ViewController1 to ViewContoller2 and vice-verse.
Unfortunately the switch from controller 1 to controller 2 occurs without any transition, but works correctly on dismiss.
I use this code in ViewController1:
let ctrl = ViewController2(nibName: nil, bundle: nil)
ctrl.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.presentViewController(ctrl, animated: true, completion: nil)
and this code in ViewControler2:
self.presentingViewController.dismissViewControllerAnimated(true, completion: nil)