iOS Swift自定义单元格导航控制器
我在TableView中有CollectionView。一切都好,但是。当我想将我的单元格导航到另一个viewController时,出现了一个错误iOS Swift自定义单元格导航控制器,ios,objective-c,swift,uitableview,uicollectionviewcell,Ios,Objective C,Swift,Uitableview,Uicollectionviewcell,我在TableView中有CollectionView。一切都好,但是。当我想将我的单元格导航到另一个viewController时,出现了一个错误 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){ let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainB
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let bookView: BookSingleController = storyboard.instantiateViewControllerWithIdentifier("BookSingle") as! BookSingleController
self.navigationController?.pushViewController(bookView, animated: true)
bookView.bookID = "\(self.books[indexPath.row].id)"
print(self.books[indexPath.row].id)
}
Xcode在self.navigationController?.pushViewController(bookView,动画:true)行上显示错误。这是错误描述:
Value of 'RelatedBookTableViewCell' has no member 'navigationController'
RelatedBookTableViewCell是我的自定义单元格类:
class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
我的问题在哪里
谢谢。尝试在应用程序中从根控制器推送VC
UIApplication.sharedApplication().keyWindow?.rootViewController
尝试从应用程序中的根控制器推送VC
UIApplication.sharedApplication().keyWindow?.rootViewController
使用下面的代码,您可以获得Top viewcontroller,并使用该视图控制器执行推送操作
let objViewController = UIApplication.topViewController()!
使用此扩展名
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
使用下面的代码,您可以获得Top viewcontroller,并使用该视图控制器执行推送操作
let objViewController = UIApplication.topViewController()!
使用此扩展名
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
您需要:
didSelectItemAtIndexPath
传递给显示单元格的UITableViewController作为委托的单元格上的自定义委托/协议。这可以在cellForRowAtIndexPath中设置
迅捷的
自定义协议
var delegate: DisplayBookDelegate?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate.displayBook("1")
}
protocoldisplaybookdelegate{
func displayBook(bookId:字符串)
}
在tableViewController中
class tableViewController: UITableViewController, DisplayBookDelegate {
...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.delegate = self
return UITableViewCell()
}
func displayBook(let bookId) {
self.navigationController?.pushViewController(bookView, animated: true)
}
}
单元格中的
var delegate: DisplayBookDelegate?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate.displayBook("1")
}
bookId
[[NSNotificationCenter defaultCenter]postNotificationName:“kDisplayBookView”对象:@{“bookId”:“1”}]代码>
swift
NSNotificationCenter.defaultCenter().postNotificationName(“kDisplayBookView”,对象:[“bookId”:“1”)您需要:
didSelectItemAtIndexPath
传递给显示单元格的UITableViewController作为委托的单元格上的自定义委托/协议。这可以在cellForRowAtIndexPath中设置
迅捷的
自定义协议
var delegate: DisplayBookDelegate?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate.displayBook("1")
}
protocoldisplaybookdelegate{
func displayBook(bookId:字符串)
}
在tableViewController中
class tableViewController: UITableViewController, DisplayBookDelegate {
...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.delegate = self
return UITableViewCell()
}
func displayBook(let bookId) {
self.navigationController?.pushViewController(bookView, animated: true)
}
}
单元格中的
var delegate: DisplayBookDelegate?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate.displayBook("1")
}
bookId
[[NSNotificationCenter defaultCenter]postNotificationName:“kDisplayBookView”对象:@{“bookId”:“1”}]代码>
swift
NSNotificationCenter.defaultCenter().postNotificationName(“kDisplayBookView”,object:[“bookId”:“1”])您可以使用自定义委托,以便UICollectionView告诉UITableView,UITableView将告诉其UINavigationController(通过UIViewController)一个单元格不应充当其他单元格的数据源或委托。这是管理员的工作。如果要保留此设置,请将单元格选择委托给UIViewController
实例。单元格没有导航控制器属性。@Larme我在我的RelatedBookTableViewCell类中使用了UINavigationControllerDeleteGate,但错误仍然存在。您可以使用自定义委托,以便UICollectionView告诉UITableView,UITableView将告诉它的UINavigationController(通过UIViewController)一个单元格不应充当其他单元格的数据源或委托。这是管理员的工作。如果要保留此设置,请将单元格选择委托给UIViewController
实例。单元格没有导航控制器属性。@Larme我在我的RelatedBookTableViewCell类中使用了UINavigationControllerDelegate,但错误仍然存在。谢谢亲爱的Edder Núñez,您能解释一下吗?嵌套UIViews时,很难跟踪导航控制器。但是您可以从应用程序本身的代码中的任何地方访问root,因为它是单例的。在那里你可以推VCs。谢谢亲爱的Edder Núñez,你能解释更多吗?当你嵌套UIView时,很难跟踪导航控制器。但是您可以从应用程序本身的代码中的任何地方访问root,因为它是单例的。在那里你可以推风投。