Root View Controller Swift
Asked Answered
S

3

0

I have a function in my app delegate which appends an item to an array in my a file named ViewController.swift, and then I want to reload a tableview in that same file. I attempt to do so like so:

let vc = ViewController()
let navigationController = UINavigationController(rootViewController: vc)
window!.rootViewController = navigationController
println(vc.messages)

//1        vc.messages.append(message.data.message as! String)
//2        vc.MessageTableView?.reloadData()

The lines numbered 1,2 are where I set breakpoints. It appears to break after line numbered 2.

The exact error I get is

fatal error: unexpectedly found nil while unwrapping an Optional value

I believe my problem is that I am setting the root view controller incorrectly.

Edit #1

Window is declared up top.

If i run my code like this:

    let vc = ViewController()
    messages.append(message.data.message as! String)
    vc.MessageTableView?.reloadData()
    println(messages)

Then I can see that the correct data is put into the array (Which i made global for this scenario) but the table does not get updated in the View controller.

Sorenson answered 25/6, 2015 at 22:49 Comment(2)
Where is window declared? Initialized?Cu
Assuming you have an instance of a UIWindow, then the only possible reason is that there is a nil value for either message, message.data, or message.ata, message as the error indicates.Caravansary
U
0

What is the implementation of your delegate function? After you put the tableview in the storyboard,You have to connect the tableview to the ViewController.swift. Control drag from the tableview to the top of the viewcontroller (the yellowcircle) , there will be a black popup , do that two times and for each time click on datasource and delegate. Then in the viewcontroller.swift file implement the functions they show in this tutorial. You don't have to modify the AppDelegate.swift file!

https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/

Untwist answered 25/6, 2015 at 23:0 Comment(5)
Which protocol do I need? Currently all i have is @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate,Sorenson
you should put all your functions in ViewController.swift , and if you want to reload after the first load you can have a button to trigger a function that calls reload . But for the first time you want to view your tableview just implement the tableview in the ViewController.swiftUntwist
How many scenes do you have? is just one uiviewcontroller with a uitableview inside of it ?Untwist
Yep thats it. One viewController with a UItableview in it. However these functions do not work the same in the viewcontroller and must be in the app delegate.Sorenson
Why can't they be in the same viewcontroller? Do you mean you have tried them in the viewcontroller and they don't work? Or You have reason why you don't want them outside of the viewcontroller?Untwist
G
0

You have to reload data inside your ViewController Put the reload inside viewDidAppear() or sth like that

It's an error because you just init a controller, but your views is not ready yet, they are being initialized. After viewDidLoad, you can call your views, but not before.

So that's why your table is nil

Beside that, why do you need to reload your table even when you don't see it, right ?

Greasewood answered 25/6, 2015 at 23:6 Comment(1)
This did not fix it. and I do see the table this function exists in my app delegate not another view.Sorenson
U
0

if you want you viewcontroller to be the rootviewcontroller al you have to do is click on the viewcontroller in storyboard and then in the utilities-> attributes inspector -> check the box "is initial view controller"

https://www.dropbox.com/s/qfd9jqeos7ueq1w/Screen%20Shot%202015-06-25%20at%207.40.46%20PM.png?dl=0

Untwist answered 25/6, 2015 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.