获取使用故事板创建的 viewcontroller 的属性
Get attribute of viewcontroller created with storyboard
我目前正在学习 swift 并且我正在尝试从故事板作品中学习实例化,但是我现在面临的错误并没有太多记录。
我在主情节提要中创建了一个 viewcontroller 并将其指定为自定义类型 class 我之前调用过 SimpleNewsViewController,这是我的class的代码,并不复杂:
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()
}
}
在我的主要故事板上,这里是我指定的自定义 ViewController:
My Storyboard implementation
现在的问题是:在我的代码中,我实例化了我的 ViewController 感谢 instanciateViewController(identifier: "NewsView") 然后我尝试设置我的 3 个属性,如这段代码:
...
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)
...
如果我取消对这三行的注释,我会收到一条错误消息:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
IDE 还显示错误代码和线程(如果有帮助的话):
The error
在实例化控制器后插座尚未连接,您必须声明临时变量并在 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
}
我目前正在学习 swift 并且我正在尝试从故事板作品中学习实例化,但是我现在面临的错误并没有太多记录。
我在主情节提要中创建了一个 viewcontroller 并将其指定为自定义类型 class 我之前调用过 SimpleNewsViewController,这是我的class的代码,并不复杂:
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()
}
}
在我的主要故事板上,这里是我指定的自定义 ViewController: My Storyboard implementation
现在的问题是:在我的代码中,我实例化了我的 ViewController 感谢 instanciateViewController(identifier: "NewsView") 然后我尝试设置我的 3 个属性,如这段代码:
...
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)
...
如果我取消对这三行的注释,我会收到一条错误消息:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
IDE 还显示错误代码和线程(如果有帮助的话): The error
在实例化控制器后插座尚未连接,您必须声明临时变量并在 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
}