2017-07-28 103 views
0

这是我正在尝试做的事情。从CollectionViewCell调用NavigationController(以推送另一个视图控制器)

class UserProfileHeader: UICollectionViewCell { 

    lazy var button: UIButton = { 
     let button = UIButton(type: .system) 
     //... 
     button.addTarget(self, action: #selector(handleButtonTapped), for: touchUpInside) 
     return button 
    }() 

    func handleButtonTapped() { 
     print("action working now") 
     // How to push another vc here? 
    } 

    // ... 
} 

我想调用一个navigationController来在collectionViewCell中推送(显示)另一个ViewController。我怎样才能访问?我试过了,没有工作。是否因为MVC模式?细胞不能与其他人交谈?或者有什么办法可以做到吗?像使用委托/协议?我对这些事情不太了解。

回答

0

创建一个自定义委托并设置其视图控制器,然后当你的CollectionView单元格按钮挖掘称为是委托方法,它会调用你的ViewController里面就有你可以把视图控制器

func handleButtonTapped() { 
//Your delegate method showuld be invoked here 
delegate?.buttontappFromCollectionViewCell() 

} 

//Now inside your view controller 
func buttontappFromCollectionViewCell() { 
let vc = UIStoryboard(name: "storyboardName", bundle: nil).instantiateViewController(withIdentifier: "yourViewControllerIdentifier") 
let navigationController = UINavigationController(rootViewController: vc) 
navigationController.pushViewController(vc, animated: true) 
} 
+0

不知道为什么这被拒绝了。 – NRitH

+0

可能是我要求直接从UICollectionViewCell打开storybaord。但最好的办法是,如果可能的话,总是在ViewController中打开 –

+0

我同意。在故事板中连接一个动作会更容易。 – NRitH

相关问题