简体   繁体   English

根视图控制器Swift

[英]Root View Controller Swift

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. 我的应用程序委托中有一个函数,该函数将一个项目追加到名为ViewController.swift的文件中的数组中,然后我想在该文件中重新加载tableview。 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. 第1,2行是我设置断点的地方。 It appears to break after line numbered 2. 它似乎在编号2的行后中断。

The exact error I get is 我得到的确切错误是

fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:解开Optional值时意外发现nil

I believe my problem is that I am setting the root view controller incorrectly. 我相信我的问题是我错误地设置了根视图控制器。

Edit #1 编辑#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. 然后,我可以看到将正确的数据放入了数组(在这种情况下我将其设置为全局),但是该表在View控制器中没有得到更新。

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. 将表视图放入情节提要中之后,必须将表视图连接到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. 将控件从tableview拖动到viewcontroller的顶部(黄色圆圈),将出现一个黑色弹出窗口,执行两次,每次单击数据源并委托。 Then in the viewcontroller.swift file implement the functions they show in this tutorial. 然后在viewcontroller.swift文件中实现它们在本教程中显示的功能。 You don't have to modify the AppDelegate.swift file! 您无需修改​​AppDelegate.swift文件!

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

You have to reload data inside your ViewController Put the reload inside viewDidAppear() or sth like that 您必须在ViewController重新加载数据将重新加载放入viewDidAppear()或诸如此类

It's an error because you just init a controller, but your views is not ready yet, they are being initialized. 这是一个错误,因为您只是init一个控制器,但是您的views尚未准备好,它们正在初始化。 After viewDidLoad , you can call your views , but not before. viewDidLoad之后,您可以调用views ,但不能在之前调用。

So that's why your table is nil 所以这就是你的table nil的原因

Beside that, why do you need to reload your table even when you don't see it, right ? 除此之外,为什么即使您没有看到table也需要重新加载table ,对吗?

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" 如果您希望viewcontroller成为rootviewcontroller,那么您只需在情节提要中单击viewcontroller,然后在Utility-> Attributes inspector->选中“ is initial view controller”框即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM