首页 > 解决方案 > 导航按钮需要从最后一个视图转到第一个视图

问题描述

项目的可视化视图

您好,我提供了上面的链接,以显示我要完成的工作的直观视图。因此,每个按钮都是下一个视图的延续,但最后一个按钮不是预期的。由于导航控制器,最后两个视图在顶部自动包含后退按钮,我想让这些后退按钮转到第一个视图控制器。我怎么能用 xcode 和 swift 做到这一点?

标签: swiftxcode

解决方案


如果要删除所有导航控制器,请使用此代码

将此代码放在按钮选择器方法中

self.navigationController!.viewControllers.removeAll()

或者

navigationController?.popToRootViewController(animated: true)

或者

如果您没有导航控制器并使用dismissViewController 方法,您仍然可以使用:

view.window?.rootViewController?.dismiss(animated: true, completion: nil)

现在是返回按钮代码:

override func viewDidLoad() {
    super.viewDidLoad()
    var backbutton = UIButton(type: .Custom)
    backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Any image can be used. download back button image from any site or whatever you wanna use.
    backbutton.setTitle("Back", forState: .Normal)
    backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
    backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}

func backAction() -> Void {        
   //here put code from above whichever you wanna use for example I'm using one

   self.navigationController?.popToRootViewController(animated: true)
}

推荐阅读