swiftuser123
swiftuser123

Reputation: 43

segue back with NavigationBar

Hello I am in Viewcontroller B with its NavigationBar, button pressed in Viewcontroller B goes to Viewcontroller C (I don't want NavigationBar in Viewcontroller C)

let vc = self.storyboard?.instantiateViewController(withIdentifier: "viewC") as! ViewcontrollerC
    vc.passAction = "saveedit"
    vc.passName = passName
    self.present(vc, animated: true, completion: nil)

When I click save Button in ViewcontrollerC I should go back to ViewcontrollerB with ViewcontrollerB NavigationBar

let vc = self.storyboard?.instantiateViewController(withIdentifier: "viewB") as! ViewcontrollerB
            vc.passName = "\(firstNameTxt.text!)\(" ")\(lastNameTxt.text!)"
             self.present(vc, animated: false, completion: nil)

Here the problem is When I go back to ViewcontrollerB I don't see NavigationBar there.

EDITED

class ViewcontrollerB : UpdateDataDelegate {

  override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        self.navigationController?.isNavigationBarHidden = false
    }


@IBAction func click_edit(_ sender: Any) {

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "patientPersonalData") as! patientPersonalDataVC
    vc.passName = passName
    vc.passAction = "saveedit"

    self.navigationController?.pushViewController(vc, animated: true)

  }  

func loadData() {

}
}

//ViewcontrollerC

protocol UpdateDataDelegate {
    func loadData()
}
 class Viewcontroller C {
 var delegate: UpdateDataDelegate?

 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
    }

fun click_save() {
self.navigationController?.popViewController(animated: true)
 self.delegate?.loadData()
}

}

Upvotes: 0

Views: 81

Answers (5)

Roen
Roen

Reputation: 116

I just have posted code dedicated on UINavigationBar appearance management on github. check out RRViewControllerExtension, it will solve your problem gracefully. with RRViewControllerExtension all you have to do is just override method in your viewcontroller.

//override any of the methods below in your viewcontroller's .m file to make specific navigation bar appearance

-(BOOL)prefersNavigationBarHidden;
-(BOOL)prefersNavigationBarTransparent;

-(nullable UIColor *)preferredNavatationBarColor;
-(nullable UIColor *)preferredNavigationItemColor;
-(nullable UIImage *)preferredNavigationBarBackgroundImage;
-(nullable NSDictionary *)preferredNavigationTitleTextAttributes;

Upvotes: 0

Preeti Rani
Preeti Rani

Reputation: 695

Try this .. unhide navigationBar in viewWillAppear of viewControllerC and hide it in viewWillAppear of viewControllerB .

//ViewController C
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}

//ViewController B
 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = false
}

try this then

//ViewController C
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
 self.navigationController?.setNavigationBarHidden(true, animated: true)
}

//ViewController B
 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

Upvotes: -1

PGDev
PGDev

Reputation: 24341

Why don't you simply dismiss ViewcontrollerC on pressing Save button?

In ViewcontrollerC:

var closure: ((String)->())? //Set this closure when you present ViewcontrollerC from ViewcontrollerB

func save()
{
    self.dismiss(animated: true) {[weak self] in
        self?.closure?("Your_Data")
    }
}

This will move you back to ViewControllerB where the navigation bar is already present.

You don't need to hide/show the navigation bar anywhere, neither while presenting ViewControllerC, nor when dismissing it.

Upvotes: 1

Ved Rauniyar
Ved Rauniyar

Reputation: 1589

Try to present it using navigation controller

let vc = self.storyboard!.instantiateViewController(withIdentifier: "viewC") as! ViewcontrollerC
let navController = UINavigationController(rootViewController: vc)
self.present(navController, animated:true, completion: nil)

and add the following line in your ViewcontrollerC's viewWillAppear method.

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}

when leave from ViewcontrollerC's viewWillDisAppear method.

override func viewWillDisAppear(_ animated: Bool) {
super.viewWillDisAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}

Upvotes: 0

ZHZ
ZHZ

Reputation: 2098

It seems that you create a new ViewcontrollerB. Try this to go back to ViewcontrollerB.

self.dismiss(animated: true, completion: nil)

Upvotes: 1

Related Questions