I want to assign button tag to variable. Then I trying send this data to SecondViewController and assign it to var.
#IBAction func goToProducts(_ sender: UIButton) {
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
categoryNumber = sender.tag }
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = categoryNumber
}
}
At ProductsViewController I create var but there isn't this data. I print this variable and I get this:
categoryNumber: 373
categoryNumb: Optional(24)
Can you tell me haw can I do this?
You need to assign it first before performSegue
categoryNumber = sender.tag
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
OR use sender
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: sender.tag)
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = sender as! Int
}
}
Related
My issue here is that I would like to change a UILabel's text according to which button triggered the segue. My identifier of my QuestionViewController (second view controller) is "goToQuestion", I have 2 buttons a truth and a dare button, they're both UI Buttons that can perform segues to the QuestionViewController, now the main question is how can I update the my UILabel to say Truth if the segue was performed through the truthPressed UIButton and to say dare if segue was performed through darePressed, I'm sure it requires and if and else statement that has to be in the QuestionViewController, or an " if segue.identifier = darePressed ... " I'm just confused and would like someone to enlighten me
#IBAction func truthPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToQuestion", sender: self)
}
#IBAction func darePressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToQuestion", sender: self)
}
You need to override prepare(for segue:
#IBAction func truthPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToQuestion", sender: "truth")
}
#IBAction func darePressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToQuestion", sender: "dare")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let des = segue.destination as? QuestionViewController {
des.sentValue = sender as! String
}
}
With
class QuestionViewController:UIViewController {
var sentValue = ""
override func viewDidLoad() {
super.viewDidLoad()
print(sentValue)
}
}
I'm trying to send details from selected cell in a NSTableView to another ViewController.
I have a button:
#IBAction func detailButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "toDetailsVC", sender: self)
}
And a prepare for segue:
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
let row = self.tableView.row(for: sender as! NSButton)
if segue.identifier == "toDetailsVC" {
let destination = segue.destinationController as! DetailsVeiwController
destination.name = orders[row].name!
}
}
The error:
Could not cast value of type 'Test.ViewController' (0x100022b98) to 'NSButton' (0x7fff8b59b260).
When I check let row = self.tableView.row(for: sender as! NSButton) inside the button, it works fine.
Am I way off here? Where is this going wrong?
self is the view controller. You have to pass the sender which is the button. And I recommend to declare the sender parameter as the concrete type
#IBAction func detailButtonTapped(_ sender: NSButton) {
performSegue(withIdentifier: "toDetailsVC", sender: sender)
}
I have a button that after tapping, perform a segue, in this perform, I send the tag of UIButton, with the sender
#IBAction func nextBtn(_ sender: UIButton) {
performSegue(withIdentifier: "more", sender: sender.tag)
}
and in the prepare segue, I try to use this tags, but appearntly i am doing it wrong
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "more" {
if let navigationVC = segue.destination as? UINavigationController, let openPDF = navigationVC.topViewController as? Morepage {
let senderTag = (sender as? UIButton)?.tag
if senderTag == 1 {
print(123)
} else if senderTag == 2 {
print(123)
}
}
}
}
The senderTag is shown as none or nil. could you help me to use sender.tag properly in the prepare segue.
many thanks
You can check your button tag following way. sender: will be sender not sender.tag
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "more" {
if let navigationVC = segue.destination as? UINavigationController, let openPDF = navigationVC.topViewController as? Morepage {
if yourButton.tag == 1 {
print(123)
} else if yourButton.tag == 2 {
print(123)
}
}
}
}
OR
if sender.tag == 1 {
print("Button 1 is pressed")
}
I want to get the objectId from parse and pass it through segue. But the objectId is passing over as an empty string:
class QueryViewController: UIViewController {
var objectIdFormParse = String()
#IBAction func makeQueryButtonTapped(_ sender: UIButton) {
makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
}
//Successfully saved
} else {
//Error, check error
}
}
performSegue(withIdentifier: resultsSegue, sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
destination.objectIdFromQueryVC = objectIdForomParse
}
}
}
The print statement prints the objectId correctly, but the segue passes empty.
You can pass the data through perform segue. just change perform segue line with following one:
performSegue(withIdentifier: resultsSegue, sender: objectIdForomParse)
And in your prepare for segue method add following lines:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
let objectIDParse = sender as! String
destination.objectIdFromQueryVC = objectIDParse
}
}
}
Perform segue when your error is nil since you are setting objectIdForomParse
only when your error is nil.
Update :
#IBAction func makeQueryButtonTapped(_ sender: UIButton) {
makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
performSegue(withIdentifier: resultsSegue, sender: self)
}
//Successfully saved
} else {
//Error, check error
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
destination.objectIdFromQueryVC = objectIdForomParse
}
}
}
I have a problem with my second viewController when trying to send data with segue. The code of Login is:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondView = segue.destination as! DataBaseConn
secondView.user=TextUsername.text!
secondView.pass=Pass.text!
}
override func viewDidLoad() {
super.viewDidLoad()
self.performSegue(withIdentifier: "sendData", sender: self)
}
I get the error at this line:
let secondView = segue.destination as! DataBaseConn
Both views have the custom class set.