谁能解释一下 backBarButtonItem Action 是如何被覆盖的?

发布于 2024-11-14 03:26:59 字数 1109 浏览 4 评论 0原文

我曾经问过这个问题。但还没有找到解决办法 这是我的场景。

//RootViewController.m
[self.navigationController pushViewController:view1 Animated:YES];

这是 View1.m

UIBarButtonItem *back_of_view2 = [[UIBarButtonItem alloc]initWithTitle:@"Back to RootViewfromview2"
target:self
action:@selector(backClicked:)];
[self.navigationController pushViewController:view2 animated:YES];

这是 backClicked 方法:(仍在 view1.m 中)

-(void)backClicked:(UIBarButtonItem *)back_of_view2
{
[self.navigationController popToRootViewController Animated:YES]
}

我能够将 view2 中的后退按钮作为“back to RootViewfromview2”,但是 backClicked 操作不会被调用,当我单击后退时,我得到了 view1 ,这是默认操作。 但是,当我在 view2.m 中设置 leftBarButtonItem 时,即

UIBarButtonItem *backtoOne = [[UIBarButtonItem alloc]initWithTitle:@"JumptorootView" style:UIBarButtonItemStyleBordered target:self action:@selector(someMethod:)];

-(void)someMethod:(UIBarButtonItem *)backtoOne  
{
[self.navigationController popToRootViewController animated:YES];
}

现在..,如果我使用 leftBarButtonItem..,一切正常,但我不会得到后箭头..

I have asked this question sometime back. But havent got a solution yet,
Here is my scenario.

//RootViewController.m
[self.navigationController pushViewController:view1 Animated:YES];

Here is View1.m

UIBarButtonItem *back_of_view2 = [[UIBarButtonItem alloc]initWithTitle:@"Back to RootViewfromview2"
target:self
action:@selector(backClicked:)];
[self.navigationController pushViewController:view2 animated:YES];

And here is the backClicked method: (still in view1.m)

-(void)backClicked:(UIBarButtonItem *)back_of_view2
{
[self.navigationController popToRootViewController Animated:YES]
}

I am able to get the back button in view2 as "back to RootViewfromview2" but the action backClicked is not getting called and when I click on back, I am getting the view1 , which is the default action.
But, when I set the leftBarButtonItem in view2.m i.e

UIBarButtonItem *backtoOne = [[UIBarButtonItem alloc]initWithTitle:@"JumptorootView" style:UIBarButtonItemStyleBordered target:self action:@selector(someMethod:)];

-(void)someMethod:(UIBarButtonItem *)backtoOne  
{
[self.navigationController popToRootViewController animated:YES];
}

Now.., if i use leftBarButtonItem..,Everthing works fine, but I wont get the back Arrow..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2024-11-21 03:26:59

您可以使用像默认导航栏中显示的后退按钮一样的图像。如果您覆盖导航栏上的后退按钮,就像您在上面的代码中所做的那样,那么您将不会获得默认的后退按钮,而是创建一个像后退一样的图像按钮并将该图像分配给左栏按钮项目。

希望有帮助......

You can use the image like the back button shown in default navigation bar.If you override the back button on navigation bar , as you are doing in above code , then you will not get the default back button, instead create an image like a back button and assign that image to your left bar button item.

Hope it helps.......

猫性小仙女 2024-11-21 03:26:59

处理这种情况的最佳方法是当 viewController2 即将从导航堆栈中弹出时,通过委托协议向 viewController1 发送消息。在此示例中,我假设 viewController2 是类 DetailViewController 的实例。

@interface DetailViewController : UIViewController
@property (nonatomic, weak) id <DetailViewControllerDelegate> delegate;
@end

@protocol DetailViewControllerDelegate <NSObject>
- (void)detailViewControllerDidFinish:(DetailViewController *)detailViewController
@end

@implementation DetailViewController

@synthesize delegate = _delegate;

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    if ([self isMovingFromParentViewController]) {
        [self.delegate detailViewControllerDidFinish:self];
    }
}

@end

The best way to handle this situation is for the viewController2 to send viewController1 a message via a delegate protocol when it's about to be popped off the navigation stack. In this sample, I'm assuming viewController2 is an instance of class DetailViewController.

@interface DetailViewController : UIViewController
@property (nonatomic, weak) id <DetailViewControllerDelegate> delegate;
@end

@protocol DetailViewControllerDelegate <NSObject>
- (void)detailViewControllerDidFinish:(DetailViewController *)detailViewController
@end

@implementation DetailViewController

@synthesize delegate = _delegate;

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    if ([self isMovingFromParentViewController]) {
        [self.delegate detailViewControllerDidFinish:self];
    }
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文