Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

ios - Get attribute of viewcontroller created with storyboard

I'm currently learning swift and I'm trying to learn of instanciation from storyboard works, but the error I'm facing now isn't documented very much.

I created a viewcontroller in my main storyboard and I specified it's type as a custom class I called previously SimpleNewsViewController, here's the code of my class, it is'nt complicated:

class SimpleNewsViewController: UIViewController {

@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myText: UITextView!

var event: Events!

override func viewDidLoad() {
    super.viewDidLoad()
}

}

On my main storyboard here is the custom ViewController I specified : My Storyboard implementation

Now here's the problem : In my code I instanciate my ViewController thanks to the instanciateViewController(identifier: "NewsView") and then I try to set my 3 attributes like in this piece of code :

...
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "NewsView") as! SimpleNewsViewController
    //controller.myImage.image = UIImage(named: "image.jpg")
    //controller.myText.text = "this is an example that can be really long"
    //controller.myTitle.text = "this is a title example
    self.navigationController?.pushViewController(controller, animated: true)
...

If I un-comment the three lines I have an error saying:

Fatal error: Unexpectedly found nil while unwrapping an Optional value

The IDE also displays the error code and the thread (if that can help): The error

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Right after instantiating the controller the outlets are not connected yet, you have to declare temporary variables and set the outlet properties in viewDidLoad()

...
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "NewsView") as! SimpleNewsViewController
    controller.tempImage = UIImage(named: "image.jpg")
    controller.tempLabel = "this is an example that can be really long"
    controller.tempText = "this is a title example
    self.navigationController?.pushViewController(controller, animated: true)
...

class SimpleNewsViewController: UIViewController {

@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myText: UITextView!

var tempImage : UIImage?
var tempLabel = ""
var tempText = ""

var event: Events!

override func viewDidLoad() {
    super.viewDidLoad()
    myImage.image = tempImage
    myLabel.text = tempLabel
    tempText.text = tempText
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...