任何人都可以解释如何覆盖backBarButtonItem动作?

时间:2011-06-06 06:18:31

标签: iphone objective-c uitableview uinavigationcontroller uibarbuttonitem

我已经问过这个问题了。但还没有解决方案, 这是我的情景。

//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中的后退按钮作为“返回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 ..,Everthing工作正常,但我不会得到后箭头..

2 个答案:

答案 0 :(得分:2)

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

希望它有所帮助.......

答案 1 :(得分:1)

处理这种情况的最佳方法是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