OK, i have 2 problem here:
1
I try to load a ViewController from a button in the xib file.
This is my button from my xib: UIView.
#IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
self.presentViewController(vc, animated: true, completion: nil)
}
The problem here is the presentViewController because my xib is a UIView type, his show me this error: "(value type ViewController) has no member of presentViewController".
Why i try to use this code? is because in my CalendarioViewController i have this code when pressed a button: dismiss(animated: true, completion: nil) so this is my first problem...
2
The second problem is when i have this code in a button inside of my xib: UIView, the CalendarioViewController is load ok...but the code: dismiss(animated: true, completion: nil) is not working anymore.
Here my code of my button xib:
#IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
self.window!.rootViewController = vc
}
So..how can load a ViewController (CalendarioViewController) and the dismiss code Keep running?
ok... i have what i need.
For this work i use UITapGestureRecognizer in View Controller which holds the view:
In override func viewDidLoad() put this code:
let fechaArriboTap = UITapGestureRecognizer(target: self, action:#selector(handleArribo))
let fechaSalidaTap = UITapGestureRecognizer(target: self, action:#selector(handleSalida))
fechaArribo.addGestureRecognizer(fechaArriboTap)
fechaSalida.addGestureRecognizer(fechaSalidaTap)
Then create the methods:
func handleArribo() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}
func handleSalida() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}
and thats is! Thanks #sasquatch
Related
I am not able to see the navigation bar in a view controller while presenting it from another view controller
I have tried keeping
self.navigationController?.navigationBar.isHidden = false
and also tried keeping a navigation controller to the view controller(embedded in).
Controller A:-
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController =
storyboard.instantiateViewController(withIdentifier:"Identifier") as! B
present(viewController, animated: true, completion: nil)
controller B:-
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = false
}
Try this
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let ViewController =
storyboard.instantiateViewController(withIdentifier:"Identifier") as! B
let vc = UINavigationController(rootViewController: ViewController)
present(vc, animated: true, completion: nil)
If you are doing it programmatically then it will help you:-
SWIFT 4
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC
self.navigationController?.pushViewController(vc!, animated: true)
In the first view controller viewWillAppear(), add this:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
In the second one, add this:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
}
override func viewDidDisappear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
Happy Codingđ
I'm trying to fix an onboarding/walkthrough to my app. Everything works as it should, except that when I navigate to the first page, navigationController disappears.
When I close the app and start it again, there is NavigationController/bar there.
is there something wrong in my code?
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(mainVC, animated: true, completion: nil)
}
You need to present your VC with a UINavigationController, or push a new VC onto the current navigationController.
First approach pushing mainVC to current navigationController (Probably will work better in your case):
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.navigationController?.pushViewController(mainVC, animated: true)
}
Second approach, presenting with initializing a navigationController:
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(UINavigationController(rootViewController: mainVC), animated: true, completion: nil)
}
I'm trying to push to different viewcontroller programatically using this code:
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
Here is my setup: I have swift file called: MenuController which contains this code I set the identifier name to: LoginViewController I set the class of both ViewControllers set to MenuController. But whenever I press the button to trigger the event it doesn't do anything.
Here is the rest of my code as requested:
import UIKit
class MenuController: UIViewController {
var thingy = 0
#IBAction func ThingyBttn(_ sender: UIButton) {
if(thingy == 1) {
print("ok")
}else{
print("good")
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
}
}
#IBOutlet var myButton: UIButton!
#IBAction func Button(_ sender: UIButton) {
if let ButtonImage = myButton.image(for: .normal),
let Image = UIImage(named: "ButtonAppuyer.png"),
UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image) {
thingy = 1
print("1")
} else {
thingy = 2
print("2")
}
}
}
I know that everything else with the code works fine because it's printing the message.
You need to insert the vc you do the push from inside a navigationController as this
self.navigationController?
is nil , or do it programmatically
Edit:
Replace this
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
with
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
let nav = UINavigationController(rootViewController:self)
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = nav
nav.pushViewController(loginVC, animated: true)
I think your view controllers are not in Navigation Stack thats why its not pushing to next view controller
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: ânextvcâ) as! nextvc
let navigation = UINavigationController(rootViewController:self)
UIAppliaction.window!.rootViewController = navigation
navigation.pushViewController(nextVC, animated: true)
I am trying to move back from a ViewController (simple text page) to a main ViewController with a TabBar at the bottom using gesturing.
The gesture works as I return to the original main screen but there is no TabBar.
In the simple ViewController I use this code to go back to the originating ViewController.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewControllerID")
self.present(controller, animated: true, completion: nil)
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewControllerID") as? AboutViewController
{
present(vc, animated: true, completion: nil)
}
default:
break
}
}
}
In the ViewController with the TabBarController I have tried the following lines to rejuvinate the TabBarController but without any joy.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
tabBarController?.tabBar.isHidden = false
NSLog("TabBar true")
}
Any ideas?
First if you want to go-back don't use present as it will add the same VCS twice in the stack , you have to use unwindSegue/dismiss , or load the tabBar itself with id
self.dismiss(animated: true) {
// use this if it's not directly behind the dismissed VC
let tab = storyboard.instantiateViewController(withIdentifier: "tabBarID") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tab
}
//
I have a login view controller which on a correct login will open a tabbar controller with the following method:
self.dismissViewControllerAnimated(true, completion: { self.loadHomeScreen()})
func loadHomeScreen()
{
emailField.text = ""
passwordField.text = ""
self.presentViewController(UIStoryboard.tabbarController()!, animated: true, completion: nil)
}
private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
class func tabbarController() -> UITabBarController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("TabbarControllerID") as? UITabBarController
}
}
I think my login view controller is still in the background? I have a logout button as the rightbarbutton item on each navbar in my tabgroup. I want to be able to close the tabbar on this button press. How can I achieve this. I can't find any examples. Would I be using a pop command?
UPDATE:
#IBAction func tryLogout(sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(true, completion: nil)
let storyboard = UIStoryboard(name: "main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("login") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}