簡體   English   中英

UINavigationBar BarButtomItem不消除視圖控制器迅速

[英]UINavigationBar BarButtomItem not dismissing the view controller swift

我已經以編程方式創建了UINavigationBar並添加了barbuttonitem,問題是當我單擊該圖像時,相應的方法正在調用,但視圖控制器未關閉。

LocateVehicle

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

我正在導航以從tableview單元查看控制器

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單元格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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM