gpt4 book ai didi

ios - UINavigationBar BarButtomItem 不会 swift 解除 View Controller

转载 作者:行者123 更新时间:2023-11-28 15:12:50 25 4
gpt4 key购买 nike

我以编程方式创建了 UINavigationBar 并添加了 barbuttonitem,问题是当我单击该图像时,相应的方法正在调用但 View Controller 没有关闭。

定位车辆

class LocateVehicle: UITableViewController{

let kCloseCellHeight: CGFloat = 130
let kOpenCellHeight: CGFloat = 488
let kRowsCount = 10
var cellHeights: [CGFloat] = []

let dataArr = ["Running", "Stopped", "Idle", "Running"]

@IBAction func backBarButton(_ sender: Any) {
}

override func viewDidLoad() {
super.viewDidLoad()

tableView.contentInset.top = UIApplication.shared.statusBarFrame.height

setup()
}

private func setup() {
cellHeights = Array(repeating: kCloseCellHeight, count: kRowsCount)
tableView.estimatedRowHeight = kCloseCellHeight
tableView.rowHeight = UITableViewAutomaticDimension
tableView.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "background"))
}

}
// MARK: - TableView
extension LocateVehicle {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArr.count
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard case let cell as CustomLocateVehicleCell = cell else {
return
}

cell.backgroundColor = .clear

if cellHeights[indexPath.row] == kCloseCellHeight {
cell.unfold(false, animated: false, completion: nil)
} else {
cell.unfold(true, animated: false, completion: nil)
}

cell.number = dataArr[indexPath.row]
// cell.statusLabel.text = dataArr[indexPath.row]
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoldingCell", for: indexPath) as! CustomLocateVehicleCell

let durations: [TimeInterval] = [0.26, 0.2, 0.2]
cell.durationsForExpandedState = durations
cell.durationsForCollapsedState = durations

// cell.liveTrackButton.tag = indexPath.row;


return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeights[indexPath.row]
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let cell = tableView.cellForRow(at: indexPath) as! FoldingCell

if cell.isAnimating() {
return
}

var duration = 0.0
let cellIsCollapsed = cellHeights[indexPath.row] == kCloseCellHeight
if cellIsCollapsed {
cellHeights[indexPath.row] = kOpenCellHeight
cell.unfold(true, animated: true, completion: nil)
duration = 0.5
} else {
cellHeights[indexPath.row] = kCloseCellHeight
cell.unfold(false, animated: true, completion: nil)
duration = 0.8
}

UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: { () -> Void in
tableView.beginUpdates()
tableView.endUpdates()

}, completion: nil)

}
}

实时跟踪

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setNavigationBar()
}

func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: screenSize.width, height: 44))
self.view.addSubview(navBar);
let navItem = UINavigationItem(title: "AP 16 BD 5678");
let image = UIImage(named: "ic_chevron_left_white")?.withRenderingMode(.alwaysOriginal)
let doneItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.backAction));
navItem.leftBarButtonItem = doneItem;
navBar.setItems([navItem], animated: false);
}

@IBAction func backAction(_ sender: Any) {
print("back working")
//self.navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}

我正在从 tableview 单元格导航到 View Controller

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let view = storyboard.instantiateViewController(withIdentifier: "LiveTrackStoryboard") as! LiveTrack
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//show window
appDelegate.window?.rootViewController = view

请帮我解决这个问题。提前致谢

最佳答案

cellForRowAt中添加按钮目标

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoldingCell", for: indexPath) as! CustomLocateVehicleCell

let durations: [TimeInterval] = [0.26, 0.2, 0.2]
cell.durationsForExpandedState = durations
cell.durationsForCollapsedState = durations
cell.yourButton.addTarget(self, action:#selector(buttonClicked()), for: .touchUpInside)
return cell
}

在单元格按钮操作上执行 Push(您正在创建第二个 Controller 根 Controller ,所以没有什么可以返回,所以不要这样做而是执行 push,并将 Controller 添加到堆栈)确保您的 root controller 已嵌入到 navigationController。 -:

func buttonClicked(sender:UIButton){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let view = storyboard.instantiateViewController(withIdentifier: "LiveTrackStoryboard") as! LiveTrack
// TO PUSH
navigationController?.pushViewController(view,animated: true)
// TO PRESENT
present(view!, animated: true, completion: nil)
}

现在在第二个 Controller 中你有 leftBarButton 弹出到前一个 Controller -:

 override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setNavigationBar()
}

func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: screenSize.width, height: 44))
self.view.addSubview(navBar);
let navItem = UINavigationItem(title: "AP 16 BD 5678");
let image = UIImage(named: "ic_chevron_left_white")?.withRenderingMode(.alwaysOriginal)
let doneItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.backAction));
navItem.leftBarButtonItem = doneItem;
navBar.setItems([navItem], animated: false);
}

@IBAction func backAction(_ sender: Any) {
print("back working")
// TO POP
self.navigationController?.popViewController(animated: true)
// TO DISMISS
dismiss(animated: true, completion: nil)
}

根据您的需要执行任何操作。

如果您不想执行推送,您也不需要将 navigationController 嵌入为 root。

关于ios - UINavigationBar BarButtomItem 不会 swift 解除 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47363330/

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