以编程方式从 collectionView Cell didSelectItemAt 推送到新的 ViewController - swift
Pushing to new ViewController from collectionView Cell didSelectItemAt programmatically - swift
我试图创建一个以编程方式实现 UICollectionViewController
的简单应用程序。我已经创建了一些单元格,当用户点击特定单元格时应该移动到另一个控制器。但是我面临着哪个视图没有推送到下一个视图控制器的问题。
这是我在 appDelegate
中设置 rootViewController
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let flow = UICollectionViewFlowLayout()
let collectionView = ViewController(collectionViewLayout: flow)
let navigation = UINavigationController(rootViewController: collectionView)
window?.rootViewController = navigation
我是这样创建的UICollectionView
private let cellId = "cellid"
var viewController: ViewController?
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
// Do any additional setup after loading the view, typically from a nib.
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = .red
return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 210)
}
这是我在用户点击特定
时的控制方式
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item)
viewController?.pushView()
}
func pushView() {
let controller = UINavigationController(rootViewController: DetailController())
self.navigationController?.pushViewController(controller, animated: true)
}
即使函数被调用,但它永远不会推送到新的视图控制器
改变
viewController?.pushView()
到
self.pushView()
请试试这个:
let objMainController : yourViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourViewController") as! yourViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = objMainController
请从情节提要中嵌入导航控制器。它可能会帮助 you.Thank 你。
首先你必须确保你当前的视图控制器有导航视图控制器,你可以通过在 viewDidLoad 方法 print(self.navigationViewController) 中打印对象来检查。如果打印 nil 那么你必须按照这些步骤制作导航控制器 -
SelectYourVicotroller -> Go to Editor 菜单选项 -> Embed to -> Navigation Controller
func pushView() {
let controller = DetailController()
//OR
//Add the storyboard identifier of your view controller - "DetailController"
let controller = self.storyboard.instantiateViewController(withIdentifier: "DetailController") as! DetailController
self.navigationController?.pushViewController(controller, animated:true)
}
将此方法调用到didSelect方法中
我试图创建一个以编程方式实现 UICollectionViewController
的简单应用程序。我已经创建了一些单元格,当用户点击特定单元格时应该移动到另一个控制器。但是我面临着哪个视图没有推送到下一个视图控制器的问题。
这是我在 appDelegate
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let flow = UICollectionViewFlowLayout()
let collectionView = ViewController(collectionViewLayout: flow)
let navigation = UINavigationController(rootViewController: collectionView)
window?.rootViewController = navigation
我是这样创建的UICollectionView
private let cellId = "cellid"
var viewController: ViewController?
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
// Do any additional setup after loading the view, typically from a nib.
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = .red
return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 210)
}
这是我在用户点击特定
时的控制方式override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item)
viewController?.pushView()
}
func pushView() {
let controller = UINavigationController(rootViewController: DetailController())
self.navigationController?.pushViewController(controller, animated: true)
}
即使函数被调用,但它永远不会推送到新的视图控制器
改变
viewController?.pushView()
到
self.pushView()
请试试这个:
let objMainController : yourViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourViewController") as! yourViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = objMainController
请从情节提要中嵌入导航控制器。它可能会帮助 you.Thank 你。
首先你必须确保你当前的视图控制器有导航视图控制器,你可以通过在 viewDidLoad 方法 print(self.navigationViewController) 中打印对象来检查。如果打印 nil 那么你必须按照这些步骤制作导航控制器 - SelectYourVicotroller -> Go to Editor 菜单选项 -> Embed to -> Navigation Controller
func pushView() {
let controller = DetailController()
//OR
//Add the storyboard identifier of your view controller - "DetailController"
let controller = self.storyboard.instantiateViewController(withIdentifier: "DetailController") as! DetailController
self.navigationController?.pushViewController(controller, animated:true)
}
将此方法调用到didSelect方法中