Don't reload VC1 when clicking back from CV2 - Swift

空扰寡人 提交于 2019-12-11 17:16:31

问题


I would like to navigate from VC1 (All Restaurant View) to VC2 (Restaurant Details View) and when ill press "Back Button" it shouldn't reload VC1 again.

How can I solve this?

func clickNameButtonCollectionView(sender: UIButton) {

        let restaurent_Id = ((self.allRecommendedRestaurent[sender.tag]  as AnyObject).value(forKey: "id") as AnyObject) as? Int

        let obj = self.storyboard?.instantiateViewController(withIdentifier: "ResturantDetailsController") as! ResturantDetailsController
        obj.restaurent_ID = restaurent_Id!
        self.navigationController?.pushViewController(obj, animated: true)
    }


@IBAction func backPressed(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }

Added:

override func viewDidLoad() {
        super.viewDidLoad()

        self.refreshControl.addTarget(self, action: #selector(self.reloadJoinedData), for: UIControlEvents.valueChanged)
        self.mainScrollView?.addSubview(refreshControl)

        self.appDel.apiManager.setCurrentViewController(vc: self)

        // Do any additional setup after loading the view.

        resturantTable.delegate = self
        resturantTable.dataSource = self
        resturantTable.bounces = false
        resturantcollection.delegate = self
        resturantcollection.dataSource = self
        resturantcollection.bounces = false

回答1:


You can use block to perform any action after back button press. Follow below code

1) Create block in your ResturantDetailsController

var back_block : (() -> Void)? = nil

2) Update your back button action backPressed

@IBAction func backPressed(_ sender: Any) {
    if let action = back_block {
        action()
    }
    self.navigationController?.popViewController(animated: true)
}

3) Now in VC1 when you create ResturantDetailsController object.

    let obj = ResturantDetailsController.loadController()
    obj.back_block = {
        //reload Your TableView
    }
    obj.restaurent_ID = restaurent_Id!
    self.navigationController?.pushViewController(obj, animated: true) 



回答2:


If you are using UITableView in VC1 then reload it in viewWillAppear then you will get updated or this is you refresh your list

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    self.tableview.reloadData() 

}



回答3:


Please write this code in viewWillAppear method in VC1 class:

 self.view.setNeedsDisplay()

It may helps you.Thank you.



来源:https://stackoverflow.com/questions/54215641/dont-reload-vc1-when-clicking-back-from-cv2-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!