-2

My Desire Output:

i want to pass data with direct initializing of a second viewController

in my first vc :

class MainViewController : UIViewController {

    @IBAction func nextQuestion(_ sender: UIButton ) {

        //enter code here
         questionIndex += 1
         let currentQuestion = exam1[questionIndex]
         questionLabel.text = currentQuestion.question
        //enter code here
        let svc = SecondViewController()
        svc.label.text = "some data from mainvc"  
}

in my secondvc :

class SecondViewController : UIViewController {
    @IBOutlet weak var label : UILabel! 
}

// any error is not showed up when I write code an the simulator works. but when it comes to sending data to svc app crashes and gives error " found nil while unwrapping optional"

nikano
  • 1,136
  • 1
  • 8
  • 18
Samm Hadji
  • 15
  • 7

1 Answers1

0

The problem is that when you do:

let svc = SecondViewController()

You are initializing without the information you put in the Storyboard (assuming you are using storyboards). Hence, so the UILabel is never initialized.

Solution

In order to solve that replace the line:

let svc = SecondViewController()

for

let svc = UIStoryboard(name: "YourStoryboardName", bundle: nil).instantiateViewController(withIdentifier: "YourViewIdentifier") as! SecondViewController

nikano
  • 1,136
  • 1
  • 8
  • 18
  • If you have any problem finding the UIViewController indentifier let me know – nikano Mar 27 '18 at 09:59
  • When i wrote that code it gives error as consecutive declarations on a line must be seperated. Because of SecondViewController() when it fixes SecondViewController;() and code doesnt work again. İ can not pass a single data to next vc. Why is that. İ van do it with segue. But i must do it without segue or connection between two vc – Samm Hadji Mar 27 '18 at 10:30
  • When a button is triggered i want to send data to next vc. But i dont wanna see the next vc because i want the button to do some other task and at the same time send data to next vc. İs that possible?? – Samm Hadji Mar 27 '18 at 10:37
  • I have two viewcontrollers and tab bar controller and so there is no connection between vc. Just a simple quiz or exam app. When i pust next question button there comes next question's string it is ok. But when it comes to sending the answer data to next vc problem happens. – Samm Hadji Mar 27 '18 at 10:40
  • @SammHadji Answering your first comment. Yes, you have to replace `let svc = SecondViewController()` for the code I posted. – nikano Mar 27 '18 at 10:47
  • @SammHadji you will have to forgive me, but I don't really get your problem... Can you edit your question adding some more code to it? I will try to help you – nikano Mar 27 '18 at 10:49
  • I tried to edit it. as you see when button is trigerred next question comes. As this happens I want that button to send info to secondVc about which answer was chosen. and change the label's text in the second vc as chosen answer for example "A" – Samm Hadji Mar 27 '18 at 11:15