john doe john doe - 7 months ago 61
Swift Question

Loading UITableViewController Programmatically Without Storyboards

I am trying to launch a

UITableViewController
as a RootController from
AppDelegate
without using Storyboards.

Here is my TasksTableViewController:

class TasksTableViewController: UITableViewController {

override func viewDidLoad() {

super.viewDidLoad()

}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

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


The nib file is called
TasksTableViewController
. The view property of the
TasksTableViewController
is hooked up to a
UITableView
control on the nib.

The
AppDelegate
looks like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

window = UIWindow(frame: UIScreen.mainScreen().bounds)

let controller = TasksTableViewController(nibName: "TasksTableViewController", bundle: nil)
window?.rootViewController = controller

window?.makeKeyAndVisible()
return true
}


All I see is a black screen. There is no data binding to the
UITableView
control but I was hoping to see an empty uiTableview control and not the black screen. What am I doing wrong?

UPDATE:

Here is the code that got it working:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)


let controller = TasksTableViewController(nibName: "TasksTableViewController", bundle: nil)

if let window = self.window {
window.backgroundColor = UIColor.blueColor()
window.rootViewController = controller
window.makeKeyAndVisible()
}

return true
}

Answer

Try something of this sort:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let controller = TasksTableViewController(nibName: "TasksTableViewController", bundle: nil)

     if let window = self.window{
        window.rootViewController = controller
     }
}

Put in break points to confirm that it was created properly before adding it to the window. Also, there isn't a reason to instantiate a new window upon launch so don't do that.. At this point double check that the VC is in fact displayed by putting a breakpoint in viewDidAppear. Consider making TasksTableViewController a subclass of UIViewController that has a tableview inside it as this is common practice. Subclassing UITableView is usually not recommended nor is using it as a ViewController. If you laid out a custom tableview in a xib that you want to use, you can instantiate one in view did load or init on the root view controller, add it as a subview, and set the frame to cover up the entire screen. I highly recommend this approach.