Search code examples
swiftxcodeswift3

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


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

Solution

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