0

我正在尝试更改特定后退按钮的操作,但由于某种原因它没有调用我给他的方法。这很奇怪,因为标题确实发生了变化,所以我认为它也应该适用于该动作。这是我的按钮代码:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle: @"Back"
                                   style: UIBarButtonItemStyleBordered
                                   target:self action: @selector(goBackToChooseMovie)];

[self.navigationItem setBackBarButtonItem: backButton];

这是我的方法:

-(void)goBackToChooseMovie{

    ViewController *VC = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0];
    VC.showingPhotoLibraryFromCamera = YES;

    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popToRootViewControllerAnimated:NO];

}
4

2 回答 2

1

这永远不会起作用,因为您无法更改后退按钮的性质。苹果对他们提供的组件有预定义的规则。有两种选择:

1) 如果您只想调用自定义方法,则可以创建一个带有“后退”图像的自定义按钮,然后将其分配给 leftBarButtonItem。

UIImage* image = [UIImage imageNamed:@"backButton.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *back = [[UIButton alloc] initWithFrame:frame];
[back setBackgroundImage:image forState:UIControlStateNormal];
[back addTarget:self action:@selector(goBackToChooseMovie) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.leftBarButtonItem = leftBarButton;

2)您可以使用-viewWillDisappear

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
       // back button was pressed. 
    }
    [super viewWillDisappear:animated];
}
于 2013-06-30T06:22:15.813 回答
0

你应该改变你的代码

[self.navigationItem setBackBarButtonItem: backButton];

self.navigationItem.leftBarButtonItem = backButton;

希望对你有效。

谢谢

于 2013-06-30T06:19:44.307 回答