我试图创建一个简单的应用程序,该程序以编程方式实现AppDelegate
。我创建了一些单元格,当用户单击特定单元格时,应将其移至另一个控制器。但是我面临的问题是哪个视图没有推送到下一个视图控制器。
这是我在UICollectionViewController
中设置rootViewController
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)
}
即使调用了该函数,但也不会推送到新的视图控制器
答案 0 :(得分:1)
首先,您必须确保当前的视图控制器具有导航视图控制器,您可以通过在viewDidLoad方法print(self.navigationViewController)中打印对象来进行检查。如果打印为零,则必须使导航控制器执行以下步骤- 选择您的Vicotroller->转到“编辑器”菜单选项->嵌入到->导航控制器
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方法
答案 1 :(得分:0)
更改
viewController?.pushView()
收件人
self.pushView()
答案 2 :(得分:0)
请尝试以下操作:
let objMainController : yourViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourViewController") as! yourViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = objMainController
请从情节提要中嵌入导航控制器。可能会对您有所帮助。谢谢。