简体   繁体   English

iPhone-更改UINavigationController上“后退”按钮的目标或选择器

[英]iPhone - Change target or selector for Back Button on UINavigationController

The default behaviour when pushing a UIViewController on a UINavigationController is for the OS to display a back button that pops the UIViewController off again. 在UINavigationController上按下UIViewController时,默认行为是使OS显示一个后退按钮,该按钮会再次弹出UIViewController。

I have the desire to set a different behavior for this back button (to go back two screens) - is there anyway I can do this without having to create my own back button with custom graphic etc. 我希望为此后退按钮设置不同的行为(返回两个屏幕)-无论如何,我都可以执行此操作而不必使用自定义图形等创建自己的后退按钮。

Thanks :) 谢谢 :)

As I half suspected originally, this isn't possible any exceptionally easy way. 正如我最初怀疑的那样,这是不可能的任何特别简单的方法。 So same method applies when creating any custom UIBarButtonItem, just have to source the back button icon from Google.... 因此,在创建任何自定义UIBarButtonItem时应用相同的方法,只需从Google采购后退按钮图标即可。...

UIButton *backButtonInternal = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,30)];
[backButtonInternal setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
boldSystemFontOfSize:12]];
[backButtonInternal addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonInternal];   
[backButtonInternal release];
[[self navigationItem] setLeftBarButtonItem:backBarButton];
[backBarButton release];

Using the "leftBarButtonItem" allows you to set the target and selector. 使用“ leftBarButtonItem”可以设置目标和选择器。 But if you set the "backBarButtonItem" on the previous controller, the target and selector will be ignored. 但是,如果您在前一个控制器上设置了“ backBarButtonItem”,则目标和选择器将被忽略。 However, the leftBarButtonItem does not have the left pointing arrow. 但是,leftBarButtonItem没有向左的箭头。

If you subclass your navigation controller, you can implement the popViewControllerAnimated: method, and throw an isKindOfClass: check in there to determine if the view controller you're looking for is being popped. 如果将导航控制器子类化,则可以实现popViewControllerAnimated:方法,并抛出isKindOfClass:在那里检查以确定是否正在弹出您要查找的视图控制器。 Eg: 例如:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    //Reference current controller being displayed
    UIViewController *currentController = [self.viewControllers lastObject];

    //Check class
    if ([currentController isKindOfClass:[MyDesiredController class]]) {
        NSLog(@"Popping Desired Controller, Do Stuff Here");
    }

    return [super popViewControllerAnimated:animated];
}

However this does not cancel the actual popping of the view controller (returning nil will stop the controller from popping but will still cause the navigation bar to pop it's information, and returning NO to the shouldPop: delegate method of the navigation bar will still pop the controller regardless. I have heard that this only occurs when using a Navigation Controller, but I haven't tested this). 但是,这并不会取消视图控制器的实际弹出操作(返回nil会阻止该控制器弹出,但仍会导致导航栏弹出其信息,并且向导航栏的ShouldPop:委托方法返回NO仍会弹出该视图控制器我听说这仅在使用导航控制器时发生,但我尚未对此进行测试)。

For your situation however, since you desire to pop two view controllers back, you could possibly remove the second last view controller from the navigation controller's viewcontrollers property by converting the viewcontrollers to an nsmutablearray, removing the controller, and then converting this nsmutablearray back to an array and setting it as the navigation controller's viewcontrollers property. 但是,根据您的情况,由于您希望向后弹出两个视图控制器,因此您可以通过将视图控制器转换为nsmutablearray,删除控制器,然后将该nsmutablearray转换回一个导航控制器的viewcontrollers属性中删除倒数第二个视图控制器。数组,并将其设置为导航控制器的viewcontrollers属性。 I haven't tested this but I thought I would share the idea. 我尚未对此进行测试,但我想我会分享这个想法。

Anything wrong with UIViewController's navigationItem property? UIViewController的navigationItem属性有问题吗? Here's how I get a cancel button, for example: 例如,这是我获得取消按钮的方法:

self.navigationItem.leftBarButtonItem =
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel
                                               target: self
                                               action: @selector(cancel)] autorelease];  

In parent's viewcontroller, 在父母的视图控制器中,

- (void)viewDidLoad
{    
    self.navigationController.delegate= self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self)
    {
        // your codes
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM