Connect menu of 5 buttons to a view, with a single segue

1

How to use a single segue (1 segue for 5 buttons) instead of 5 segues (1 per button) leaving only one, which contains all the IBAaction and takes you to the other view depending on what you press, the content in the other view is presented in one way or another.

I have been trying the following code but without result.

Origin

    @IBAction func abstractionENG(sender: UIButton) { return performSegueWithIdentifier("menuENG", sender: nil) } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if(segue.identifier == "menuENG")
{ if let destinationViewController = segue.destinationViewController as? DefinitionViewController{ 
destinationViewController.tittle = DiccioModel.abstraction().nameEng destinationViewController.def = DiccioModel.abstraction().descriptionEng destinationViewController.link = DiccioModel.abstraction().linkEng } }

Destination

import UIKit

class DefinitionViewController : UIViewController {
    @IBOutlet weak var name:UILabel!
    @IBOutlet weak var bodyText:UITextView!
    var tittle:String!
    var def:String!
    var link:String!

    /*This IBAction activates hyperlink to Wikipendia */

    @IBAction func linkToWiki(sender: UIButton) {
        if let url = NSURL(string: link) { UIApplication.sharedApplication().openURL(url) }
    }

    var definition : DiccioModel!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.definition = DiccioModel.webBrowser()

        self.name.text = tittle
        self.bodyText.text = def



        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


     // End of the code...
}



//

Does anyone have any way to do it as I request it or could it guide me? I have been trying in various ways in code without result

PS: I have identified each button in the IDentity inspector with numbers to implement them in the sender but I do not achieve the result either. PS: I'm Rookie level in this so if they could be clarifying it would be great :) Greetings

    
asked by Heallz Slamer 11.08.2017 в 23:50
source

2 answers

1

Thanks for the considerations, I took them all into account and they helped me solve. Use the following code:

class MainViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

/* The following functions containts a group of conditionals which will change the scene depending  of the IBAction selected */

@IBAction func abstractionENG(sender: UIButton) {
    let data = [DiccioModel.abstraction().nameEng,DiccioModel.abstraction().descriptionEng,DiccioModel.abstraction().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func webBrowserENG(sender: UIButton) {
    let data = [DiccioModel.webBrowser().nameEng,DiccioModel.webBrowser().descriptionEng,DiccioModel.webBrowser().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func latencyENG(sender: UIButton) {
    let data = [DiccioModel.latency().nameEng,DiccioModel.latency().descriptionEng,DiccioModel.latency().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func conditionalENG(sender: UIButton) {
    let data = [DiccioModel.conditional().nameEng,DiccioModel.conditional().descriptionEng,DiccioModel.conditional().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}


@IBAction func operatingSystemENG(sender: UIButton) {
    let data = [DiccioModel.os().nameEng,DiccioModel.os().descriptionEng,DiccioModel.os().linkEng]
    performSegueWithIdentifier("menuENG", sender: data)
}

@IBAction func abstractionESP(sender: UIButton) {
    let data = [DiccioModel.abstraction().nameEsp,DiccioModel.abstraction().descriptionEsp,DiccioModel.abstraction().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}

@IBAction func webBrowserESP(sender: UIButton) {
    let data = [DiccioModel.webBrowser().nameEsp,DiccioModel.webBrowser().descriptionEsp,DiccioModel.webBrowser().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}

@IBAction func latencyESP(sender: UIButton) {
    let data = [DiccioModel.latency().nameEsp,DiccioModel.latency().descriptionEsp,DiccioModel.latency().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}

@IBAction func conditionalESP(sender: UIButton) {
    let data = [DiccioModel.conditional().nameEsp,DiccioModel.conditional().descriptionEsp,DiccioModel.conditional().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}


@IBAction func operatingSystemESP(sender: UIButton) {
    let data = [DiccioModel.os().nameEsp,DiccioModel.os().descriptionEsp,DiccioModel.os().linkEsp]
    performSegueWithIdentifier("menuESP", sender: data)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "menuENG") || (segue.identifier == "menuESP"){
        if let destinationViewController = segue.destinationViewController as? DefinitionViewController{
            if let data = sender as? Array<String>{
                destinationViewController.tittle = data[0]
                destinationViewController.def = data[1]
                destinationViewController.link = data[2]
            }
        }
    }



}

PS: This code is the main controller ie the view where the buttons are and in turn DestinationViewController, is the destination of the information where everything goes through the segue when you press a button in the English category or Spanish.

    
answered by 13.08.2017 / 08:08
source
3

What you have to do is assign a value to the tag property of each button, so that you have boton1.tag = 1, boton2.tag = 2 ..... This you do when you define the button. When you do the prepareForSegue, you get the button pressed in sender. you cast him to UIButton and from there you take the tag. Knowing the tag you know what button you have pressed and then once you have created the new driver you have to pass the button and the new controller, knowing that you have pressed will do what you have to do. For this you need a property (or the same with the ones you have created from tittle, def and link serve you) and you put the values you need. I think what you want is that it depends on the button that you press to pass a tittle, def and different link. You do that in the prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject? { 
if(segue.identifier == "menuENG"){
   if let destinationViewController = segue.destinationViewController as? DefinitionViewController { 

//aquí casteo el sender a un UIButton
if let b = sender as? UIBUtton {
    //ahora puedo saber que botón pulsaron si le asigne el tag en su momento
    switch b.tag {
    case 1:
           destinationViewController.tittle = El que sea    
           destinationViewController.def = el que sea
           destinationViewController.link = el que sea
    case 2:
           destinationViewController.tittle = El que sea    
           destinationViewController.def = el que sea
           destinationViewController.link = el que sea

   .......
   }
}
}

If you just pass the button and the other controller looks for life, because what you said you put a property to the second controller of type Int and you pass in the value of the tag (which is the button pressed) to be look for life

destinationViewController.botonPulsado = b.tag

(having casted the sender to UIButton as I have put you above)

    
answered by 12.08.2017 в 09:34