I want to switch to a new view controller when I press a button without using a navigation controller and I can't seem to be able to do this. I have looked everywhere for an answer but everything that I have found is either in objective-c, using a navigation controller, or not working at all.
func gunsoutButtonPressed(sender:UIButton!) {
print("yay\t")
//let encounterVC:AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("ecounterViewController")
//self.showViewController(encounterVC as UIViewController, sender: encounterVC)
let encounterViewController = self.storyboard.instantiateViewControllerWithIdentifier("encounterViewController") as encounterViewController
self.pushViewController(encounterViewController, animated: true)
}
You have to connect the two ViewControllers you would like to use. Then name the segue however you want and then use the following code to trigger the segue (inside an IBAction or something). It is not completely programmatically but you can trigger them programmatically, which should be enough.
performSegueWithIdentifier("mySegue", sender: nil)
Check out this video for support.
Hope this helps :)
You cannot use a push segue without a UINavigationController. You could achieve this with a modal segue, such as this:
self.presentViewController(encounterViewController, animated: true, completion: nil)
Segue
So you use the Segue ID to make sure you segue correctly.
No Segue
I would suggest doing this instead though:
let vc = ViewControllerToSegueTo()
self.presentViewController(vc, animated: true, completion: nil)
It is slightly better if you do not want to use the storyboard.
Related
I am trying to present a custom view from a controller modally, however when I do so the background of everything but the textfield I have turns black as seen in here . (the intended modal)
This seems to be a fairly common problem, however I have already implemented the code that most similar questions pose as the answer
here is my addEvent method in the first controller:
#IBAction func addEvent(_ sender: Any) {
let newEventViewController = NewEventViewController()
newEventViewController.view.backgroundColor = UIColor.clear
newEventViewController.view.isOpaque = false
navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
navigationController?.present(newEventViewController, animated: true, completion: nil)
}
Any help figuring out what I am missing would be appreciated. If it makes a difference I am creating my app using individual xib files and not a storyboard.
You need to set the modalPresentationStyle of newEventController not the navigationController like this:
newEventViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
Also although not needed I would present from self rather than the navigation controller like this:
self.present(newEventViewController, animated: true, completion: nil)
Currently making an app in swift where all elements are added programatically. how can i add segue programmatically for the action of a given button to switch from one view to the next?
Instead of creating segues programmatically, you can use this inside button action to navigate to another view controller.
let viewController = MyViewController()
self.navigationController?.pushViewController(viewController, animated: true)
You can push to your next viewcontroller without using segue as well like.
#IBAction func btnClickNextVC(_ sender: Any) {
let objSecondVc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as? ViewController2
self.navigationController?.pushViewController(objSecondVc!, animated: true)
//or you can use
present(objSecondVc, animated: true, completion: nil)
}
Note: Don't forget to take navigationcontroller in storyboard.
and give storyboard identifier of your viewcontroller same as you pass here in the code withidentifier.
So in my attached image I show my subclass I'm creating for my HOME button. I have many VCs with a HOME button and I want to connect them to this class to make them all send the user HOME
So far, I made my button to take this class, as shown in the image. My issue is I'm not able to connect my IBAction here and not sure why.... would appreciate any tips anyone can tell me about why I'm not able to connect my IBAction function to the button right now....
Another confusing thing is that although I haven't given any of the other buttons in this stackview of buttons a class, I'm actually able to connect any of my other buttons in this stackview to my IBAction... which I find odd.
class HomeButton: UIButton {
#IBAction func showHomeVC(sender: AnyObject) {
var sb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc: UINavigationController = sb.instantiateViewController(withIdentifier: "HomeNC-ID") as! UINavigationController
self.present(vc, animated: false, completion: nil)
}
}
I'm aware that I can simply go to each VC where there's a HOME button and create an IBAction from each and just paste this code in there and it will work, but I would like to do this another way where I don't have to have so much of the same code being repeated.... I would like to know what changes need to be made to make this happen.
The error that you are are getting is because you are using the class of UIButton which doesn't have the property to display a view controller. You should present it either on a UIViewController, or one of the other types.
It is linked up but you just can't present a view controller using self (UIButton)
I have two view controllers and a button in my storyboard. I don't understand why this is not pushing the view to the second view controller. I have the Storyboard ID and Class of FirstViewController, so shouldn't it work? I keep getting this error Unknown class FirstViewController in Interface Builder file. What am I doing wrong?
#IBAction func button(sender: AnyObject) {
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("FirstViewController")
self.navigationController?.pushViewController(viewController!, animated: true)
}
Just to have an answer for others -->
Two ways to solve it ->
1.Add a navigationController in the storyboard, as you cannot do pushViewController on a viewController outside navigationController's stack.
2.You can push it as a modal view controller, using self.presentViewController(viewController, animated: true, completion: nil)
This will give a modal effect, and not the normal push effect.
I have 2 navigation controllers. I want to perform segue from one to another and change navigation stack. I'm using SWRevealViewController library for my hamburger menu. When I perform segue as on picture, menu does't work. How can I perform segue correctly? (Sorry for my english) I couldn't find any post like this. I am using swift language.
here is my storyboard
https://yadi.sk/d/z_biSNaejRoGJ
You created a manuel segue from your first view controller to another. There is not a trigger(button etc.) to the initiate your segue. You have to initiate your segue with code. You can use this code for initiate your segue;
performSegueWithIdentifier("authComplete", sender: nil)
If you put this code with your any #IBAction it should be initiate your segue. Have a nice coding.
if let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewControllerID") as? ViewController {
let navController = UINavigationController(rootViewController: secondViewController)
navController.setViewControllers([secondViewController], animated:true)
self.revealViewController().setFrontViewController(navController, animated: true)