使用NavigationBar返回

时间:2017-08-10 10:07:50

标签: ios swift

你好我在Viewcontroller B中使用了NavigationBar,在Viewcontroller B中按下按钮进入Viewcontroller C(我不想在Viewcontroller C中使用NavigationBar)

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

当我在ViewcontrollerC中点击保存按钮时,我应该使用ViewcontrollerB NavigationBar返回ViewcontrollerB

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

问题在于当我回到ViewcontrollerB时,我在那里看不到NavigationBar。

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

}

5 个答案:

答案 0 :(得分:1)

您似乎在创建一个新的ViewcontrollerB。 试试这个回到ViewcontrollerB。

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

答案 1 :(得分:1)

为什么不按ViewcontrollerC按钮直接忽略Save

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

这会将您移回ViewControllerB已存在的navigation bar

您无需在任何地方隐藏/显示navigation bar,无论是在展示ViewControllerC时,还是在解除它时。

答案 2 :(得分:0)

尝试使用导航控制器来呈现它

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

并在ViewcontrollerC的viewWillAppear方法中添加以下行。

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

从ViewcontrollerC的viewWillDisAppear方法离开时。

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

答案 3 :(得分:0)

我刚刚在github上发布了专门用于UINavigationBar外观管理的代码。请查看RRViewControllerExtension,它将妥善解决您的问题。 使用RRViewControllerExtension,您要做的就是在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;

答案 4 :(得分:-1)

试试这个..在viewControllerC的viewWillAppear中取消隐藏navigationBar并将其隐藏在viewControllerB的viewWillAppear中。

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

试试这个

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