问题描述
我目前正在使用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.
我看过这里的代码:
但我正在努力在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自定义后退按钮和目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!