私は、init内で設定したnavigatorというオブジェクトを持っています。私はそれが設定されていることを確認するためにそれを破る。しかし、IBAction func、linkButtonClicked、getの呼び出し時にnavigatorを使用しようとすると、例外が発生します。どうして?オブジェクトはinitで設定されていてもnilです
class HomeCollectionViewCell: UICollectionViewCell {
let appDelegate:AppDelegate!
let navigator: Navigator!
@IBOutlet weak var linkButton: UIButton!
var destinationView:String?
var parentViewController:UIViewController?
@IBAction func linkButtonClicked(_ sender: Any) {
do {
try self.navigator.navigate(to: self.destinationView!, from: parentViewController!)
} catch {
}
}
required init?(coder aDecoder: NSCoder) {
self.appDelegate = UIApplication.shared.delegate as! AppDelegate
self.navigator = self.appDelegate.navigator
super.init(coder: aDecoder)
}
override func prepareForReuse() {
super.prepareForReuse()
// do resetting here if needed, like empty out data
linkButton.setTitle(nil, for: .normal)
}
}
最初の2つのプロパティはIUOであってはならず、そうでない場合、コンパイラは適切に初期化されていないことを示すことでこれを解決するのに役立ちます。 –