gpt4 book ai didi

ios - Swift 自定义后退按钮和目的地

转载 作者:IT王子 更新时间:2023-10-29 05:12:11 27 4
gpt4 key购买 nike

我目前在我的应用程序中使用 SWRevealViewController 作为侧边栏菜单。当我单击其中一个选项时,目标 View Controller 没有“后退”按钮,因为它不是来自正确的 View Controller (即要返回的页面)。

因此我想在目标 View Controller 上手动创建一个后退按钮,它将返回到主视图 Controller 。

我看过这里的代码:How do I manually set the "Back" destination in iOS apps

但我正在努力在 Swift 中实现它(一个接一个的错误!)。有什么帮助吗?谢谢!

编辑

我已经尝试了下面的建议,但是后退按钮没有出现。这可能与我将导航栏隐藏在其他 View 中并在目标 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)
}

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

编辑

这是来 self 的侧边栏 View Controller 的 prepareForSegue。如果有一种方法可以检查 segue 标识符“test”,那么我可以从这里设置后退按钮吗?

- (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中写出来

写这个在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)
}

关于ios - Swift 自定义后退按钮和目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com