2017-03-17 88 views
0

我想让导航控制器中的按钮回到我以前的viewcontroller。你可以看下面的图片来清楚地说明。如何获得后退按钮以返回到第一个ViewController?

请给我的想法。我花了很多钱做,但不能做到。

+0

您最初的viewCont必须嵌入'navigationController'然后创建与另一个vc连接,在push segue的情况下,您将获得inbuild选项以返回,请参阅http://stackoverflow.com/a/42672029/4003548。 – vaibhav

+0

当我这样做时,我会失去我的第二个导航控制器 –

+0

您可以通过navigationviewcontroller使用poptoroot。 –

回答

0

您可以创建自定义后退按钮并单击你必须把这个代码

@IBAction func customBack(_ sender: Any) { 
    _ = self.navigationController?.popToRootViewController(animated: true) 
} 
0

转到你的故事板并点击navigationcontroller并选择

'show navigation bar' 

它会显示您的导航栏。

但是,如果您想制作自定义按钮,只需从故事板中选择一个按钮并在其动作中添加此代码即可。

_ = navigationController?.popViewControllerAnimated(true) 
0
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") 
4

做这样的:

添加导航控制器到你的主页

storyboard image

结果

result image

+0

在我的第二页,它也有导航控制器。如果我这样做,我会失去我的第二个导航控制器。 –

+0

不,它不会丢失。相反,导航工作得很好。我会上传另一张图片来澄清。 –

+0

好大,我们怎么实现兄弟? –

0

您需要将UIbutton或UIbarButtonItem添加到导航栏。然后你需要将按钮绑定到视图控制器。按照代码..

@IBAction func backPressed(_ sender: Any) { 

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

} 

消除导航栏的解决方案。添加此代码..

override func viewDidAppear(_ animated: Bool) { 

     self.navigationController?.setNavigationBarHidden(false, animated: false) 

} 
0

故事板中最快的方式。

按住CTRL键不放。然后点击高棉喜剧标题 的按钮,然后将鼠标拖到目的地ViewController上,显示 有关高棉喜剧的详细信息。然后释放CTRL和鼠标,就是这样。在main.storyboard 转到

0

添加导航控制器编辑器 - >嵌入在 - >导航控制器

此生成按钮为你回来

相关问题