gpt4 book ai didi

ios - 如何将菜单链接到 ViewController?

转载 作者:行者123 更新时间:2023-11-30 12:45:16 25 4
gpt4 key购买 nike

我有一个以编程方式构建的菜单,我希望每个菜单项都链接到 Storyboard 中的不同 View Controller ,这样我就可以控制 UI。到目前为止我已经:

import UIKit

class Setting: NSObject {

let name: String
let imageName: String

init(name: String, imageName: String) {
self.name = name
self.imageName = imageName

}
}

class SettingsLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@available(iOS 6.0, *)
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return settings.count
}

let blackView = UIView()

let collectionView: UICollectionView = {

let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv

}()


let cellId = "cellId"
let cellHeight: CGFloat = 40

let settings: [Setting] = {

return [Setting(name: "News", imageName: "news"), Setting(name: "Settings", imageName: "settings"), Setting(name: "UI list", imageName: "list"), Setting(name: "Book", imageName: "book"), Setting(name: "Me", imageName: "me"), Setting(name: "Cancel", imageName: "close")]

}()

func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
return CGRect(x: x, y: y, width: width, height: height)
}

var TableViewController: TableViewController?


func showSettings() {

if let window = UIApplication.shared.keyWindow {

blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)

blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

window.addSubview(blackView)

window.addSubview(collectionView)

let height: CGFloat = CGFloat(settings.count) * cellHeight
let y = window.frame.height - height
collectionView.frame = CGRectMake(0, window.frame.height, window.frame.width,height)

blackView.frame = window.frame
blackView.alpha = 0

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

self.blackView.alpha = 1

self.collectionView.frame = self.CGRectMake(0, y, self.collectionView.frame.width, self.collectionView.frame.height)
}, completion: nil)
}
}

func handleDismiss() {

UIView.animate(withDuration: 0.5) {
self.blackView.alpha = 0

if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height)

}

}
}



//func numberOfSections(in collectionView: UICollectionView) -> Int {
// return 3
//}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingCell

let setting = settings[indexPath.item]
cell.setting = setting

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width,height: cellHeight)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//print(setting.name)
// handleDismiss()

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

self.blackView.alpha = 0

if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height)

}

}) { (completed: Bool) in

let setting = self.settings[indexPath.item]
if setting.name != "Cancel" {
self.TableViewController?.showControllerForSetting(setting: setting)

}


}


}

override init() {
super.init()


collectionView.dataSource = self
collectionView.delegate = self

collectionView.register(SettingCell.self, forCellWithReuseIdentifier: cellId)

}

}

所以菜单列表是从一个数组中取出的,你可以从代码中看到。我想将每个 View Controller 链接到其自己的 View Controller ,我确实遵循了此在线教程,并且该菜单系统从 View 底部向上滑动以显示自身。

最佳答案

使用以下内容更新您的collectionView(_didSelectItemAt :)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//print(setting.name)
// handleDismiss()

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

self.blackView.alpha = 0

if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height)

}

}) { (completed: Bool) in

let setting = self.settings[indexPath.item]
if setting.name != "Cancel" {
goToNextViewController(setting: setting)

}


}


}

并添加以下方法

func goToNextViewController(setting: setting){
if setting.name == "News"{
//Here implement code to go to News screen
}else if setting.name == "Settings"{
//Here implement code to go to Settings screen
}else if setting.name == "UI list"{
//Here implement code to go to UIList screen
}else if setting.name == "Book"{
//Here implement code to go to Book screen
}else if setting.name == "Me"{
//Here implement code to go to Me screen
}else{
self.TableViewController?.showControllerForSetting(setting: setting)
}
}

如果您的 showControllerForSetting 方法已经为所有其他屏幕实现了逻辑,则仅在 self.TableViewController?.showControllerForSetting(setting: setting) 中传递设置

关于ios - 如何将菜单链接到 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41719233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com