简体   繁体   English

如何在didSelectItemAt indexPath函数下显示ViewController? (没有情节提要)

[英]How to show ViewController under the didSelectItemAt indexPath function ? (without storyboard)

Have a nice weekend. 周末愉快。 I have problem about that under the didSelectItemAt indexPath function I want to go new view controller which is show details and each cell must have different view controller (it will change according to data which are came from protocol). 我在didSelectItemAt indexPath函数下遇到问题,我想转到显示详细信息的新视图控制器,并且每个单元格必须具有不同的视图控制器(它将根据来自协议的数据进行更改)。 I tried lots of way to do that but I can't figured out. 我尝试了很多方法来做到这一点,但我不知道。 I call present and push view controller codes but its also not working because navigationController code can't work under didSelectItemAt function. 我调用了present和push视图控制器代码,但它也无法正常工作,因为navigationController代码在didSelectItemAt函数下无法工作。 Lastly I tried that: 最后,我尝试了:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let homecon = HomeController()
    homecon.showDetailPage() }


func showDetailPage() {
    let newView = pageDetail()
    self.present(newView, animated: true, completion: nil)

}

(showDetailPage function from HomeController). (来自HomeController的showDetailPage函数)。 But that code also gave error which is 但是该代码也给出了错误

'UICollectionView must be initialized with a non-nil layout parameter'. “ UICollectionView必须使用非nil布局参数初始化”。

So I can't solve it. 所以我解决不了。 How can I solve it please help me. 我该如何解决,请帮助我。 Have a nice work! 辛苦了!

This is because HomeController is a subclass of UICollectionViewController and a collectionViewController can not be initialized without it's layout! 这是因为HomeControllerUICollectionViewController的子类,并且没有其布局就无法初始化collectionViewController!

So use this initializer instead: 因此,请改用以下初始化程序:

let homecon = HomeController(collectionViewLayout: UICollectionViewLayout())

Oh and it's just the start of the week here. 哦,这只是本周的开始。

暂无
暂无

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

相关问题 collectionView函数didSelectItemAt indexPath将数据传递给下一个视图控制器 - collectionView function didSelectItemAt indexPath to pass data to next view controller Xcode Swift MacOs - 隐藏/显示 windows 没有 Z1BC4A1B743127BBF1ED2E36F8 - Xcode Swift MacOs - Hide/Show windows without a StoryBoard and without ViewController 如何在没有情节提要的情况下设置ViewController的框架 - How to set the frame of a ViewController without storyboard 推送没有 storyboard 的 ViewController - push ViewController without storyboard Swift 5. 如何在按钮触摸时呈现或显示 ViewController? 没有故事板(以编程方式) - Swift 5. How present or show ViewController on button touch? Without Storyboard (programmatically) 显示 viewController 并在没有 storyboard 和 segue 编程的情况下传递数据 - Show viewController and pass data without storyboard and segue programming 如何在Swift中根据JSON ID从collectionView didSelectItemAt indexPath pushViewController(意味着indexPath应该与json id匹配) - How to pushViewController from collectionView didSelectItemAt indexPath according to json id(means indexPath should match with json id) in Swift collectionView didSelectItemAt indexPath 未调用 swift - collectionView didSelectItemAt indexPath not called swift collectionView:UICollectionView,didSelectItemAt indexPath:未执行IndexPath - collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath is not executed 没有调用collectionView didSelectItemAt indexPath - collectionView didSelectItemAt indexPath not being called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM