I would like to open new View after clicking on a button. It works, when I make it by UIButton in Main.storyboard, however, I need to make it in code, because I have to use some if-statements with login/password. I tried to make it like it was suggested in other similar questions, but it doesn't work:
#IBAction func login(_ sender: UIButton) {
performSegue(withIdentifier: "Second", sender: self)}
Id of login-view: First;
Id of the second view: Second
You can use storyboard Id
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "secondStoryboardId") as! SecondViewController
self.present(nextViewController, animated:true, completion:nil)
Your Segue doesn't look to have an identifier in the screenshot. Can you confirm. Please add an identifier to your segue from first view to second and then alter the below code with the name of segue identifier
performSegue(withIdentifier: "Segueidentifier", sender: self)}
To add identifier on the segue, click on the segue in your storyboard and then on the right hand side in the Attributes Inspector add identifier in the Storyboard Segue section
You shouldn't use storyboard ID for segues. There will be segue identifier separately. When you drag&drop segue you will get one line connected between the screens. Click on that to add segue identifier. You need to provide that segue ID here
You can see segue connection from the below image:
Set Segue identifier as below :
Select the segue and give a Identifier/Name ( Here , I've put toSecond) at Attribute Inspector
and then perform segue .
ScreenShot :
Source Code :
#IBAction func login(_ sender: UIButton) {
performSegue(withIdentifier: "toSecond", sender: self)
}
Also make sure that your segue is from the initial VC not the button...
Related
I'm working on an app where I have an error page displayed incase there is a network problem. The app has several storyboards, and this can happen anywhere.
func displayErrorPage(errorCode: ErrorCode) -> Void {
if !isDisplayingError {
DispatchQueue.main.async {
self.isDisplayingError = true
let storyboard = UIStoryboard(name: "alert", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AlertScreen") as! AlertVC
controller.errorCode = errorCode
UIApplication.shared.topViewController?.present(controller, animated: true, completion: nil)
}
}
}
I would like to use unwind to dismiss it, to clear out any views and get back to the home page. I have used this in the alert view to close, but sometimes doesn't work.
#IBAction func closeBtn(_ sender: Any) {
flowError = true
NetworkManager.shared().isDisplayingError = false
performSegue(withIdentifier: "unwindToSH", sender: self)
}
Anyone with some pointers?
Create the unwind segue IBAction in the view controller you want to unwind to.
Perhaps in your case it is HomeViewController. Add this code inside your HomeViewController
#IBAction func unwindToHomeVC(segue: UIStoryboardSegue) {}
Wire up the unwind segue - Control drag from AlertVC’s ViewController icon to exit icon in the storyboard choose the unwind segue action (unwindToHomeVC) that was created in step 1 from the list of IBActions.
Specify a segue Identifier - specify its identifier in the Attributes Inspector of the Utilities Pane. Ex: “unwindToHomeVC”
Trigger unwind segue programmatically - trigger that in the appropriate place. In your case close button action
self.performSegue(withIdentifier: "unwindToMenu", sender: self)
It will work even if you work with multiple storyboard as well, if your setup is right. I hope that this one would help you.
Thanks
I want to switch between viewController by using containerViewController.
For this I created multiple segues and viewController.
when I run
func segueIdentifierReceivedFromParent(_ identifier: String){
self.segueIdentifier = identifier
self.performSegue(withIdentifier: self.segueIdentifier, sender: nil)
}
in it's parent controller file it works fine but when I called it through other controller file it gives error
ContainerViewController: 0x7fd703707b40>) has no segue with identifier 'second''
This is how i called it in other viewController
let vc = ParentViewController()
vc.segueIdentifierReceivedFromParent("second")
Here "second" is the Identifier given to segue in storyBoard
Above code is written in AddTarget of button.
So when I tap button I get Error
If you are in same a storyBoard then you can simply do this -
Take a button in your firstVC and ctrl + drag a segue upto your secondVC. Then click on the segue. (the round thing on between two view controller). -
And set a identifier for that (here about is the identifier, you can give any name for this)-
Here is the code -
#IBAction func UserDetailVC(sender: AnyObject) {
self.performSegue(withIdentifier: "segueIdentifierName", sender: sender)
}
I just want to perform segue with a back button on top.
#IBAction func onFirst(_ sender: UIButton) {
performSegue(withIdentifier: "firstSegue", sender: self)
}
#IBAction func onSecond(_ sender: UIButton) {
performSegue(withIdentifier: "secondSegue", sender: self)
}
Let's say you have two view controllers, ViewControllerOne and ViewControllerTwo.
You could do that programmatically or using storyboards.
Since you seem to be using #IBOutlet I will assume you want to do it using storyboards.
First open up your Main.storyboard and make sure that ViewControllerOne (the controller you want to segue from) is embedded in a Navigation Controller.
If it's not, you can do that by clicking on your View Controller, then click on Editor in the top menu bar, go to Embed In and select Navigation Controller.
Then you can create a segue using the Interface Builder by doing control + click on the little yellow icon at the top of ViewControllerOne (not the Navigation Controller) & drag to ViewControllerTwo in the Interface Builder.
Now click on the segue that just got created, and type an identifier of your choice in the Attributes Inspector.
Then in your ViewControllerOne class, you can just perform the segue using the #IBOutlet as you mentioned :
#IBAction func onFirst(_ sender: UIButton) {
performSegue(withIdentifier: "your_segue_identifier", sender: self)
}
Just make sure that the segue identifiers match, and everything should be fine :)
You need redefine target and action for your current navigation item
self.navigationItem.leftBarButtonItem?.target = self
self.navigationItem.leftBarButtonItem?.action = #selector(self.onFirst(_:))
So In my 1stViewController I have this code:
#IBAction func colorDropdown(_ sender: Any) {
self.popUpColorPicker()
}
func popUpColorPicker() {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ColorPicker") as! ColorPicker
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
}
Which would pop up the 2ndViewController. Upon dismissing the Pop Up 2ndViewController, I'd like to retrieve the values I entered and use it in my 1st View Controller.
You can achieve this by either using delegate or completion handler.
Just create a delegate to handle your data on dismissing the second VC.
**
OR
**
Write a completion handler closure to get back those values in your first view controller.
Suppose A & B are two controllers and you first navigated from A to B with some data. And now you want to POP from B to A with some data.
Unwind Segues is the best and recommended way to do this.
Here are the steps.
Open A.m
define following method
#IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) {
}
open storyboard
Select B ViewController and click on ViewController outlet. press control key and drag to 'Exit' outlet and leave mouse here. In below image, selected icon is ViewController outlet and the last one with Exit sign is Exit Outlet.
You will see 'unwindSegueFromBtoA' method in a popup . Select this method .
Now you will see a segue in your view controler hierarchy in left side. You will see your created segue near StoryBoard Entry Piont in following Image.
Select this and set an identifier to it. (suggest to set the same name as method - unwindSegueFromBtoA)
Open B.m . Now, wherever you want to pop to A. use
self.performSegueWithIdentifier("unwindSegueFromBtoA", sender: dataToSend)
Now when you will pop to 'A', 'unwindSegueFromBtoA' method will be called. In unwindSegueFromBtoA of 'A' you can access any object of 'B'.
That's it..!
you can always use unwind to do some stuff upon unwinding
declare a IBAction in your first vc
var color: UIColor!
#IBAction func unwindToCheckout(segue: UIStoryboardSegue) {
//do some stuff with color
}
then create exit segue for popout viewcontroller
then you can dismiss popout like this
self.performSegueWithIdentifier("unwindToVC1", sender: selectedColor)
then in prepareForSegue
if segue.identifier == "unwindToVC1" {
(segue.destinationViewController as! FirstViewController).color = sender as! UIColor
}
also you can create delegate to reach fistviewcontroller and do some stuff which is way easier to do
I have the following storyboard: Main Storyboard
In it, several custom View Controllers are programmatically embedded in the Scroll View. In one of them, a button is present and should trigger a segue to show the "hey" screen.
I have then wrote the following code:
#IBAction func addNewDateButtonDidTouched(sender :AnyObject) {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let storyboardInit = mainStoryboard.instantiateViewControllerWithIdentifier("mainview")
storyboardInit.performSegueWithIdentifier("showNewDate", sender: self)
}
This #IBAction seems to reload the inital view controller and perform the segue correclty (Xcode doesn't return any error). But the "hey" screen doesn't show up and its viewDidLoad() doesn't load.
Any hint?
Instead of calling
storyboardInit.performSegueWithIdentifier("showNewDate", sender: self)
try
self.performSegueWithIdentifier("showNewDate", sender: self)
The storyboardInit in your code will instantiate a UIViewController that has an identifier "mainview".
But this UIViewController hasn't been added to the screen, when you're asking the controller to perform a segue.
So, I wouldn't do that. What I would do, to segue to the hey screen is either :
Create a segue from the current controller and just do this in the code : self.performSegueWithIdentifier
Instantiate the hey controller like this : instantiateViewControllerWithIdentifier, and then just do the self.presentViewController
So, to transition to a new controller, you have to start from the current controller.
if i understand your viewcontroller hierarchy correctly...
since the viewcontroller that contains the button to trigger the segue is a childviewcontroller of the viewcontroller that has the segue setup in storyboard i think you have to call presentingViewController?.performSegueWithIdentifier("showNewDate", sender: self).