0

I've been following this thread at the end the code doesn't show any errors but when I run it on my phone, the application crashes and I get a signal SIGABRT error, what could be causing it? This is my code:

FirstViewController.swift :

class FirstViewController: UIViewController, UITextFieldDelegate, setDateValueDelegate{

func setDate(value: String) {
    self.receivedDate = value
}

@IBOutlet weak var dateButton: UIButton!
var receivedDate:String = ""

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var secondVC = (segue.destinationViewController.visibleViewController as  SecondViewController)
    secondVC.delegate = self
}

SecondViewController.swift:

protocol setDateValueDelegate {
  func setDate(value: String)
}

class SecondViewController: UIViewController {

var delegate: setDateValueDelegate?
@IBOutlet weak var datePicker: UIDatePicker!

var strDate:String = ""

func datePickerChanged(datePicker:UIDatePicker) {
    var dateFormatter = NSDateFormatter()

    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

    strDate = dateFormatter.stringFromDate(datePicker.date)
    dateLabel.text = strDate

    delegate?.setDate(strDate)
}

Any help is appreciated! :)

Community
  • 1
  • 1
Omar Dlhz
  • 158
  • 12

3 Answers3

0

Since it's not actually "visible" yet, your segue.destinationViewController.visibleViewController line doesn't work to get an instance of the soon-to-be-active SecondViewController instance; and as such, I believe the problem is that you never actually set the delegate.

Try updating your prepareForSegue method by removing the visibleViewController property and simply get the segue's destinationViewController, like so:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var secondVC = segue.destinationViewController as  SecondViewController
    secondVC.delegate = self
}

Edit: Also try setting your delegate's protocol at the top of SecondViewController if you haven't already:

// set up the setDateValueDelegate protocol with the set date function
protocol setDateValueDelegate {
    func setDate(value: String)
}

class SecondViewController: UIViewController {

    var delegate: setDateValueDelegate?
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
0

So, Im going to answer my own question and at the same time answer it to any other person who want to pass data from ViewController B to ViewController A. I will try to describe every detail for making it more simple step by step.

  1. In your storyboard, give an identifier to your segue, this is done by clicking on the segue and going to the Attributes Inspector.

  2. Create a file called "protocol.swift" with the following code inside.

protocol.swift :

protocol setDateValueDelegate {
func setDate(toValue: String)
}

The "setDateValueDelegate" and "setDate" can be replaced to whatever you want but they must be consistent within the entire code.

  1. In your FirstViewController class, implement the protocol.

FirstViewController.swift:

class FirstViewController: UIViewController, setDateValueDelegate {

(The protocols name, in my case "setDateValueDelegate" must be the same as the one in protocol.swift)

  1. Add the function to your first view controller.

    func setDate(toValue:String) {  //setDate must be replaced to the name of your function in the protocol.swift file.
    var date = toValue // The value that is being received from the SecondViewController.
    
    }
    
  2. Add prepareForSegue function to your FirstViewController:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "DueDate") {
    
    var secondVC: SecondViewController = (segue.destinationViewController as SecondViewController)
    
    secondVC.delegate = self
    
    }
    

(SecondViewController in "var secondVC: SecondViewController" and in "as SecondViewController" must be replaced to your Second View Controller's name.

  1. Add the delegate in SecondViewController

    var delegate:setDateValueDelegate?
    /* As before setDateValueDelegate must be replaced to the name that
    you gave to your delegate */
    
  2. In your Second View Controller's ViewDidLoad call the delegate:

        delegate?.setDate(/* The value that you want to send to the FirstViewController*/)
    

    /* setDate must be changed to the function's name in protocol.swift */

Hope this helped to you all! :)

Omar Dlhz
  • 158
  • 12
0

I did it like this

FirstViewController:

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

    var SecondVC: SecondViewController = segue.destinationViewController as SecondViewController

    if segue.identifier == "Player2Button"{
        SecondVC.NumberOfPlayers = 2
    }else{
        SecondVC.NumberOfPlayers = 1
    }

}

SecondViewController

Just use NumberOfPlayers to do whatever you want. Hope this helps

Ace Green
  • 381
  • 5
  • 29