Linking View Controllers through button

2019-08-08 16:06发布

问题:

I'm a beginner at all of this...Having said that I've come across a point in my app where I've stalled and don't know what to do or fix next. So any answers would be appreciated!

So in my Home View Controller, I have four buttons with four different categories. Each of these categories has its own question list, but they have a common "General Question" list. The general question list has its own view controller. When you click on any of the four buttons, it brings you to the General Question view. At the bottom of this view, I have a "Next" button.

Goal: Configure the Next button to continue to one of the category's question list based on what is initially pressed in the Home View Controller.

I've connected the buttons via outlet and action in the View Controller. However, the Next button will not connect when I control + drag into the View Controller. I'm not sure where I need to put the code for this...

I was thinking that the code for the Next button might need to have some kind of conditional statement, but since it won't connect I can't even get that far.

Help!

(This is what I have) Sample Code: import UIKit import AddressBookUI import AddressBook import Foundation import CoreData import CoreGraphics import EventKit import EventKitUI import CoreFoundation

class ViewController: UIViewController {

@IBOutlet var ColorButton: UIButton!

@IBOutlet var StyleButton: UIButton!

@IBOutlet var CutButton: UIButton!

@IBOutlet var MakeupButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

var eventstore: EKEventStore!
var event: EKEvent!
weak var editViewDelegate: EKEventEditViewDelegate!




@IBAction func ColorButtonPressed(sender: UIButton) {

}

@IBAction func StyleButtonPressed(sender: UIButton) {

}

@IBAction func HaircutButtonPressed(sender: UIButton) {

}

@IBAction func MakeupButtonPressed(sender: UIButton) {

}

}

回答1:

Here is a suggested approach as shown in the code below for 2 controllers (instead of 4) for brevity. Use appropriate named segues to each of the "next processing" controllers from the common processing controller and set up a chain. Here is a link to the project file: Project file

        import UIKit



    class ViewController: UIViewController {

    var nextVcId = 0 // defines the button that is pressed



    @IBAction func unwindFromOtherControllers(segue: UIStoryboardSegue) {

    // In case you want to get back to the main VC

    }

    @IBAction func btn2Action(sender: UIButton) {

    nextVcId = 0

    self.performSegueWithIdentifier("commonSegue", sender: sender)

    }

    @IBAction func btn1Action(sender: UIButton) {

    nextVcId = 1

    self.performSegueWithIdentifier("commonSegue", sender: sender)

    }

    override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {



    let  vc = segue.destinationViewController as! CommonViewController

    vc.nextControllerId = nextVcId



    }



    }

    import UIKit



    class CommonViewController: UIViewController {

    var nextControllerId = 0



    @IBOutlet weak var StatusLabel: UILabel!

    override func viewDidLoad() {

    super.viewDidLoad()

    self.StatusLabel.text = "Common"

    commonProcessing()

    // Do any additional setup after loading the view.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    func commonProcessing() {

    // do your common processing

    if nextControllerId == 0 {

    performSegueWithIdentifier("next1Segue", sender: self)



    } else {

    performSegueWithIdentifier("next2Segue", sender: self)

    }

    }



    }

    import UIKit



    class Next1ViewController: UIViewController {



    @IBOutlet weak var statusLabel: UILabel!

    override func viewDidLoad() {

    super.viewDidLoad()

    self.statusLabel.text = "Next1"

    next1Processing()



    // Do any additional setup after loading the view.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    func next1Processing() {

    println("Next 1 Processing")

    }





    }

    import UIKit



    class Next2ViewController: UIViewController {



    @IBOutlet weak var statusLabel: UILabel!

    override func viewDidLoad() {

    super.viewDidLoad()

    statusLabel.text = "Next 2"

    next2Processing()



    // Do any additional setup after loading the view.

    }



    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }



    func next2Processing() {

    println("Next 2 Processing")

    }





    }

processing