Swift 自定义后退按钮和目的地 [英] Swift Custom Back Button and Destination

查看:16
本文介绍了Swift 自定义后退按钮和目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的应用程序中使用 SWRevealViewController 作为侧边栏菜单.当我单击其中一个选项时,目标视图控制器没有返回"按钮,因为它不是来自正确的视图控制器(即要返回的页面).

I am currently using SWRevealViewController for a sidebar menu in my app. When I click one of the options, the destination view controller doesn't have a 'back' button because it hasn't come from a proper view controller (i.e. page to go back to).

因此,我想在目标视图控制器上手动创建一个返回按钮,该按钮将返回到主视图控制器.

Therefore I am wanting to manually create a back button on the destination view controller which will go back to the home view controller.

我在这里查看了代码:如何手动设置返回"?iOS 应用中的目的地

但我正在努力在 Swift 中实现这一点(一个又一个错误!).有什么帮助吗?谢谢!

But I am struggling to implement this in Swift (one error after another!). Any help? Thanks!

编辑

我已经尝试了下面的建议,但是后退按钮没有出现.这可能与我将导航栏隐藏在其他视图中并在目标视图中执行以下操作有关:

I have tried the suggestion below, but the back button just doesn't appear. This may have something to with the fact I have the navbar hidden in other views and do the following on the destination view:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController.setNavigationBarHidden(false, animated:true)
    var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside)

    var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
    self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func popToRoot(sender:UIBarButtonItem){
    self.navigationController.popToRootViewControllerAnimated(true)
}

不确定为什么后退按钮不显示?

Not sure why the back button won't show up?

编辑

这是来自我的侧边栏视图控制器的 prepareForSegue.如果有办法检查 segue 标识符test",那么我可以从这里设置后退按钮?

This is the prepareForSegue from my sidebar view controller. If there is a way to check for the segue identifier 'test' then I can set the back button from here?

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString];


if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
    SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;

    swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {

        UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
        [navController setViewControllers: @[dvc] animated: NO ];
        [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
    };

}

}

推荐答案

你可以这样用swift写

You can write that in swift like this

编写此代码以在 navigationController

    navigationController.setNavigationBarHidden(false, animated:true)
    var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside)
    myBackButton.setTitle("YOUR TITLE", forState: UIControlState.Normal)
    myBackButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    myBackButton.sizeToFit()
    var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
     self.navigationItem.leftBarButtonItem  = myCustomBackButtonItem

这将弹出到 rootViewController

    func popToRoot(sender:UIBarButtonItem){
       self.navigationController.popToRootViewControllerAnimated(true)
    }

这篇关于Swift 自定义后退按钮和目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆