Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Swift: UI Elements equal nil when programatically creating split view controller

I am programmatically creating a split view controller using the following code when a table view cell is touched:

let rootViewController: UIViewController = RootTableViewController()
        let navVC: UINavigationController = UINavigationController(rootViewController: rootViewController)

        let detailViewController: UIViewController = DetailTableViewController()

        let splitVC: UISplitViewController = UISplitViewController()
        splitVC.viewControllers = [navVC, detailViewController]
        self.present(splitVC, animated: true, completion: nil)

but when I tap the tableViewCell I get the error: 'fatal error: unexpectedly found nil while unwrapping an Optional value' which appears to be linked to a UITextField (and all UI Elements) on the RootTableViewController. The first failure is in the viewDidLoad of RootViewController after executing the above code when a value is passed to a ui element

Error Message on UITextField

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Where exactly does this happen? Wheres the error originated? I had a similar issue where I tried to access IBOutlets before they were created by the system which caused my app to crash.

Specifically I had a UI update function which was called after setting a property of the ViewController.

I got around this by checking for nil in the update function and since it was called before viewDidLoad was called the first time, I called the update function in viewDidLoad manually to make sure that when it shows for the first time, everything is correctly updated.

UPDATE

I think I have an idea of whats going on. You created a Storyboard, setup your UI and chose your ViewControllers as the classes of the ViewControllers in the Storyboard.

If you now want to use the ViewControllers, you have to instantiate them via the Storyboard rather than manually initializing them (something like this):

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")

You also could use Segues instead of instantiating something at all. Build your complete UI using the Storyboard and then use a Segue to present the SplitViewController.

The last method I can think of is, that if you want to instantiate the ViewControllers manually and still make use of the Storyboard or a nib, you have to do some custom initialization in in the init functions of your ViewControllers (this code is from a custom view I have in a separate .xib):

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initializeSubviews()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    initializeSubviews()
}

func initializeSubviews() {
    UINib(nibName: "DatePickerKeyboard", bundle: nil).instantiate(withOwner: self, options: nil)
    addSubview(view)
    view.frame = self.bounds
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...