Here's another version of one of the most popular questions on stackoverflow. Sorry. Thank you.
I have a viewcontroller that has 30 buttons. Each of these buttons can segue to one of 20 viewcontrollers based on a variable that is sent from a previous viewcontroller.
I know how to send data from one viewcontroller to another, and I know how to have a button connect to multiple viewcontrollers dependent on a passed variable, but I don't know how to pass a variable from a button to whatever viewcontroller is specified from the variable...
My viewcontroller looks like this:
#IBAction func didTapButton(_ sender: UIButton) {
if passedvariable == "A" {
performSegue(withIdentifier: "ToA", sender: self)
if passedvariable == "B" {
performSegue(withIdentifier: "ToB", sender: self)
}
I tried adding something like this...
#IBAction func didTapButton(_ sender: UIButton) {
if passedvariable == "A" {
performSegue(withIdentifier: "ToA", sender: self)
let send = segue.destination as! AViewController
send.NewVariableToSend = (sender as! UIButton).title(for: .normal)!}
}
But that's not working... I feel like I'm close but can't connect the dots yet. Any help is greatly appreciated, thanks!
How are you accessing the actual segue from the IBAction method of your UIButton?
This approach is wrong. Use PrepareForSegue method to pass your data to next ViewController. Its a predefined method so all you need is to override this method in your ViewController class.
Try this..
Note:
guard let is used to get rid of crash if you tried to pass any nil value to your next ViewController.
#IBAction func didTapButton (_ sender: Any) {
//First get the clickedButton Object if you do not have IBOutlet of that button
guard let clickedButton = sender as? UIButton else {return}
//Pass the clicked button to Segue perform as a sender
self.performSegue(withIdentifier: "yourSegueIdentifier", sender: clickedButton)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentifier" {
//Now get you destination viewcontroller and type cast it into your desired ViewController class
guard let nextVC = segue.destination as? YourViewController else {return}
//Now convert the sender into your clicked button because you have previously set the clickedButton as sender when you try to perform this segue
guard let clickedButton = sender as? UIButton else {return}
//Now Simply assign this to nextVC
nextVC.button = clickedButton
}
}
Related
Struggling to get my viewControllers to send value from the main viewController to a second. I want it to happen on a button click, I'm going to get the value from the button and pass it to the new form. But it just isn't working.
Code for main ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func butClick(_ sender: UIButton) {
NSLog("Button Pressed : %#",[sender .currentTitle])
//var tt = [sender .currentTitle]
// Create the view controller
let vc = TimesTablesViewController(nibName: "TimesTablesViewController", bundle: nil)
vc.passedValue = "xx"
self.performSegue(withIdentifier: "pushSegue", sender: nil)
}
}
Code for second viewController called TimesTablesViewController:
class TimesTablesViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
var passedValue:String = ""
override func viewDidLoad() {
super.viewDidLoad()
titleLabel?.text = "\(passedValue) Times Table"
}
}
I've followed tutorials but can't seem to solve the problem! Thanks for any help!
Replace
self.performSegue(withIdentifier: "pushSegue", sender: nil)
with
self.present(vc,animated:true,completion:nil)
or ( if the current vc is inside a naigation )
self.navigationController?.pushViewController(vc,animated:true)
Using
self.performSegue(withIdentifier: "pushSegue", sender: nil)
is fit with storyboards not xibs and if this your case then you need to use the above line only inside the button action with implementing this method inside the source vc
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pushSegue" {
if let nextViewController = segue.destination as? TimesTablesViewController{
nextViewController.passedValue = "xx"
}
}
}
I’m assuming that the new view controller is appearing, but you’re simply not seeing the data. If so, you’re evidently using storyboards. The TimesTablesViewController(nibName:bundle:) only works if you’re using XIB/NIBs and manually presenting new view controller.
If you’re really using storyboards, simplify your butClick method:
#IBAction func butClick(_ sender: UIButton) {
NSLog("Button Pressed")
performSegue(withIdentifier: "pushSegue", sender: self)
}
But implement prepare(for:sender:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? TimesTablesViewController {
destination.passedValue = "xx"
}
}
Assuming the above fixes your problem, I might suggest a further simplification. Notably, if your butClick(_:) method is really only calling performSegue, you can segue to this next scene without any #IBAction method at all:
remove butClick(_:) entirely;
remove the connection between the button and the butClick method in IB, on the “Connections Inspector” tab in the right panel; and
control-drag from the button previously hooked up to butClick(_:) to the scene for TimesTablesViewController.
That will simplify your code further.
I am totally new to the Mac apps development. I am facing an issue since last two days but could not succeed.
My problem is the same as question Swiching between 2 diferent NSViewControllers with data.
Could you please help me to understand the process and syntax of how to move from one NSViewController to another.
I have a View controller for login where I have two fields i.e. UserId and password.
On the click of the login button a web API is called to authenticate the user and upon receiving "SUCCESS" as the response, control should be transferred from LoginViewController to ProfileViewController.
I have tried to resole this issue as per the answer of the question (link given) but I am getting an error that. "fromviewcontroller.view.superview cannot be nil."
Create a segue from LoginViewController to ProfileViewController and give an identifier to it like "showProfile".
#IBAction func loginButton(_ sender: Any) {
// Code for validating User.
if response == "SUCCESS" {
performSegue(withIdentifier: "showProfile", sender: sender)
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if segue.identifier == "showProfile" {
let profileController = segue.destinationController as! ProfileViewController
profileController.data = responseData
}
}
Not using storyboards?
#IBAction func loginButton(_ sender: Any) {
// Code for validating User.
if response == "SUCCESS" {
let profileController = ProfileViewController(nibName: "ProfileViewController", bundle: Bundle.main)
profileController.data = responseData
self.view.window!.contentViewController = profileController
}
}
You have to create a Segue in your Storyboard (Ctrl + Left click the yellow circle button above your LoginViewController and drag it to your ProfileViewController) and then name it something like "showProfile".
When you received your "SUCCESS" you want to call:
//here you enter the name of the segue you want to call
//& in sender the data you want to pass to the new VC.
performSegue(withIdentifier: "showProfile", sender: nil)
this will call
prepare(for segue: UIStoryboardSegue, sender: Any?)
in your current ViewController, so if you want to pass data to your new ViewController, you need to override it.
Example for passing data between ViewControllers :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//asking which segue is called
if segue.identifier == "showProfile" {
//when you called "showProfile", you can be sure that your
//destination is a ProfileViewController
let vc = segue.destination as! ProfileViewController
//passing the data to your new VC
vc.data = sender
}
}
I want my user to go to the TableViewController once they enter their "id" and "password" but when we use prepare and performsegue methods we just send one variable.
#IBAction func logIn(_ sender: Any) {
performSegue(withIdentifier: "emailSegue", sender: emailTextField.text)
performSegue(withIdentifier: "passwordSegue", sender: passwordTextField.text)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let guest = segue.destination as! ExpenseTableViewController
guest.incoming.append(sender as! String)
}
As you can see here I want to perform two different segues and I have also created two segues on the storyboard from ViewController to tableview controller named as "emailSegue" and "passwordSegue", but in the prepare function I can only prepare for the one of them which is the "emailSegue" when I run the code, it does compile and I can append email to the array but not my password.
So I wonder how I can get them both to be sent? Is there a way to prepare for 2 different segues? Could not find an example of it on anywhere else.
Thanks in advance.
You can only perform one segue, but you can send multiple parameters.
Just pass an array rather than a single string:
#IBAction func logIn(_ sender: Any) {
performSegue(withIdentifier: "emailSegue",
sender: [emailTextField.text!, passwordTextField.text!])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "emailSegue" {
let guest = segue.destination as! ExpenseTableViewController
guest.incoming.append(contentsOf: sender as! [String])
}
}
PS: Rather than forced unwrapping the text properties you should validate them:
#IBAction func logIn(_ sender: Any) {
guard let email = emailTextField.text, !email.isEmpty,
let password = passwordTextField.text, !password.isEmpty else {
// show an alert
return
}
performSegue(withIdentifier: "emailSegue", sender: [email, password])
}
You can only perform one segue at a time, so call performSegue only once when you want the transition to happen and set the two variables in prepareForSegue for the destination view controller. It's best to check what type of segue should be prepared by checking segue.identifier property.
I have a view controller with a lot (36) of buttons, and I want each of these buttons to segue to a particular view controller based on a variable that was set earlier in the program. In other words, any button could potentially go to 15 different view controllers based on a variable that was sent to the viewcontroller containing the buttons...
I think I can make this work if I click and drag each button to every viewcontroller... but it seems silly and messy.
I tried doing something like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if variable == "Whatever" {
let send = segue.destination as! AViewController
send.variablesent = (sender as! UIButton).title(for: .normal)!}
}
But this only works if I click and drag the button in the storyboard to the "AViewController".
Any help is appreciated, thanks!!
For that you can make segue from SourceViewController to DestinationViewController instead of from Button to Controller, After that when you call perfromSegue in your button action then pass button reference as sender in perfromSegue call.
#IBAction func buttonAction(_ sender: UIButton) {
self.performSegue(withIdentifier: "segueIdentifier", sender: sender)
}
Now in prepareForSegue cast sender to UIButton and set title according to it.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if variable == "Whatever" {
let send = segue.destination as! AViewController
if let button = sender as? UIButton {
send.variablesent = button.titleLabel?.text ?? "Default value"
}
}
}
I wanted to make a converter,
This is how it works:
On the initial screen, it asks from which unit you wanna convert,
then when the user clicks on the button it sends the title of the button as string to the next view controller,
then using the if statement and checking the the title string to the all units like inch, meter, yard, etc it identifies from which unit we are converting from,
then on 'Editing did change' function in the text field it converts and gives the answer
I have a problem when it recognises and sends the button title to the next view, what is happening is it is not carrying out the touch up inside function and directly sending the from string which doesn't carry the button title,
Here is the code
class lengthconversions: UIViewController {
var from = "From"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "answer" {
if let destinationVC = segue.destinationViewController as? lengthconversionsanswer{
destinationVC.from = from
}
}
}
#IBAction func fromunit(sender: AnyObject) {
let buttonTitle = (sender as? UIButton)?.titleLabel?.text
from = buttonTitle!
print(from)
}}
How can I do that the touch up inside function happens and then the
data transfer through the segue.
p.s - The button to which the segue is hooked is the same to which the
Touch up inside function is used
The prepareForSegue function has an argument of sender. If you are performing the segue because of a button click, then the sender will be the button.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "answer" {
if let destinationVC = segue.destinationViewController as? lengthconversionsanswer,
let button = sender as? UIButton {
destinationVC.from = button.titleLabel?.text!
}
}
}