UINavigationBar BarButtomItem 没有解除视图控制器 swift

UINavigationBar BarButtomItem not dismissing the view controller swift

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

定位车辆

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)

  }
}

LiveTrack

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)
 }

我正在从表格视图单元导航到视图控制器

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(您正在创建第二个控制器根控制器,所以没有什么可以返回,所以不要这样做而是执行推送,并将控制器添加到堆栈)确保您的 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)
    }

现在在第二个控制器中 leftBarButton 弹出到上一个控制器 -:

 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。