我想让导航控制器中的按钮回到我以前的viewcontroller
。你可以看下面的图片来清楚地说明。如何获得后退按钮以返回到第一个ViewController?
请给我的想法。我花了很多钱做,但不能做到。
我想让导航控制器中的按钮回到我以前的viewcontroller
。你可以看下面的图片来清楚地说明。如何获得后退按钮以返回到第一个ViewController?
请给我的想法。我花了很多钱做,但不能做到。
您可以创建自定义后退按钮并单击你必须把这个代码
@IBAction func customBack(_ sender: Any) {
_ = self.navigationController?.popToRootViewController(animated: true)
}
转到你的故事板并点击navigationcontroller并选择
'show navigation bar'
它会显示您的导航栏。
但是,如果您想制作自定义按钮,只需从故事板中选择一个按钮并在其动作中添加此代码即可。
_ = navigationController?.popViewControllerAnimated(true)
I prefer you to create a parent class in which you will create back button, this way you can re-use this code
1. create an Parent class
class ParentClass: UIViewController {
func addBackBtnWithImageName(string:String)-> Void
{
let button = UIButton.init(type: .custom)
button.setImage(UIImage.init(named: string), for: UIControlState.normal)
button.addTarget(self, action:#selector(backbtnClick), for: UIControlEvents.touchUpInside)
button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem.init(customView: button)
self.navigationItem.leftBarButtonItem = barButton
}
func backbtnClick() {
_ = navigationController?.popViewController(animated: true)
}
}
2. Your class where you want to add back button
class YourClass: ParentClass /*inheriting from parent class*/ {
override func viewDidLoad() {
super.viewDidLoad()
self.addBackBtnWithImageName(string: "Back")
}
}
In any class where you want to add back button, just inherit from parent class and add this line in viewdidload :
self.addBackBtnWithImageName(string: "Back")
您需要将UIbutton或UIbarButtonItem添加到导航栏。然后你需要将按钮绑定到视图控制器。按照代码..
@IBAction func backPressed(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
消除导航栏的解决方案。添加此代码..
override func viewDidAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
故事板中最快的方式。
按住CTRL键不放。然后点击高棉喜剧标题 的按钮,然后将鼠标拖到目的地ViewController上,显示 有关高棉喜剧的详细信息。然后释放CTRL和鼠标,就是这样。在main.storyboard 转到
添加导航控制器编辑器 - >嵌入在 - >导航控制器
此生成按钮为你回来
您最初的viewCont必须嵌入'navigationController'然后创建与另一个vc连接,在push segue的情况下,您将获得inbuild选项以返回,请参阅http://stackoverflow.com/a/42672029/4003548。 – vaibhav
当我这样做时,我会失去我的第二个导航控制器 –
您可以通过navigationviewcontroller使用poptoroot。 –