简体   繁体   English

当导航控制器将其作为根视图时,tableview在ViewController中似乎为nil [SWIFT 3]

[英]tableview appear to be nil in ViewController when it's root by a navigation controller [SWIFT 3]

I'm having an issue with my current project. 我目前的专案有问题。 At the beginning my main focus was to create a overlay button in a TableViewController but i saw that it was more simple to use a ViewController and set on it a tableView : 一开始,我的主要重点是在TableViewController中创建一个叠加按钮,但我发现使用ViewController并在其上设置tableView更简单:

class MemoList : UIViewController, UITableViewDataSource, UITableViewDelegate{

@IBOutlet weak var myTableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        myTableView.delegate = self
        #datasource is linked manually in the StoryBoard
        ...
    }
} 

When this View Controller is set as the initial controller, everything work fine but in my case, when the projet is rooted by a navigation controller, the tableview appears to be nil in the context of the 'Memolist' (when pushing on it). 当将此View Controller设置为初始控制器时,一切正常,但就我而言,当projet由导航控制器作为根目录时,在“记事簿”的上下文中(推入时)表视图似乎为零。

Do you have any idea about this issue ? 您对这个问题有任何想法吗? And how can I solve it ? 我该如何解决呢?

Thanks, 谢谢,

For me the problem was that I used 对我来说,问题是我曾经

present(myViewController, animated: true, completion: nil)

After I tried to use segue and 在我尝试使用segue和

performSegue(withIdentifier: "myIdentifier", sender: self)

it worked properly and tableView was no longer nil. 它工作正常,tableView不再为零。

Why did I have this problem: When you use present() , you initialise the View Controller from code and it doesn't watch on your Storyboard . 为什么会有这个问题:当您使用present() ,您会从代码初始化View Controller ,并且它不会在Storyboard上监视。 When you use segues , it uses the Storyboard and of course see all you did in your Interface Builder 当您使用segues ,它将使用Storyboard并且当然会看到您在Interface Builder所做的一切

You also can: 您还可以:

UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerId")

This creates view controller from Storyboard, so that tableView is initialized now 这将从Storyboard创建视图控制器,以便立即初始化tableView

暂无
暂无

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

相关问题 连接到导航控制器时如何将ViewController弹出到根目录 - How to pop ViewController to root when connected to navigation controller 导航控制器未出现在仪表板ViewController上 - Navigation Controller doesn't appear on Dashboard ViewController 弹出到根导航控制器 - Swift 3 - Popping to root navigation controller - Swift 3 当自定义viewController作为根视图控制器而不是Navigation Controller作为self.window.rootViewController添加时,视图不会加载 - View is not loaded when custom viewController added as a root view controller instead of Navigation Controller as the self.window.rootViewController 斯威夫特:tableView总是在viewcontroller内“找到nil而unwr…” - Swift: tableView always “found nil while unwr…” inside a viewcontroller 导航控制器show / push viewcontroller swift - Navigation Controller show/push viewcontroller swift 如何在没有情节提要ID的情况下迅速为导航控制器设置根视图控制器。 并导航到其他视图,仅使用viewcontroller名称 - How to set root view controller for a navigation controller in swift without storyboard id. And navigate to other view Only with viewcontroller name 当在 Swift 5 中点击 Tableview Row 时,如何使用 Xib(不是 StoryBoards)在 SideMenu Controller 中设置 UINavigationController 以推送新的 ViewController - How to set UINavigationController in SideMenu Controller using Xib (Not StoryBoards) to push new ViewController when Tableview Row Tapped in Swift 5 通过导航在 ViewController 之间传递数据到另一个 ViewController controller Swift 5 - Passing data between a ViewController to another ViewController with navigation controller Swift 5 使用TableView或CollectionView时的Swift,Slim控制器代码 - Swift, Slim Controller's Codes When Using TableView or CollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM