2017-03-17 100 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

添加導航控制器編輯器 - >嵌入在 - >導航控制器

此生成按鈕爲你回來

相關問題