代码之家  ›  专栏  ›  技术社区  ›  Adam Eberbach Adil Shaikh

abpeoplePickerNavigationController-删除“取消”按钮而不使用私有方法/属性?

  •  5
  • Adam Eberbach Adil Shaikh  · 技术社区  · 15 年前

    我使用的是abpeoplePickerNavigationController,它是uinavigationController的一个子类,在上下文中,我使用的是右侧的默认导航栏按钮“取消”,没有任何意义。我找不到一种方法来禁用或隐藏它,无论使用什么方法,都需要是公共的和可存储的。 完全摆脱导航栏(picker.navigationbarhidden=yes;)可能是一个选项,除非在弹出到联系人列表后,导航栏重新出现。 子类化abpeoplePickerNavigationController和截取视图将显示为尝试,而nil“取消”按钮不起作用。 [Picker setallowsCancel:no];可以工作,但没有文档,所以我希望永远不会通过审批。

    10 回复  |  直到 11 年前
        1
  •  4
  •   pheelicks    13 年前

    这一个

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
      UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,0.0f,0.0f)]; 
      UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom]; 
      //UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)]; 
      [viewController.navigationItem setRightBarButtonItem:btn animated:NO]; 
      [btn release]; 
      [custom release]; 
    }
    

    工作完美!

        2
  •  2
  •   David Hunt    13 年前

    本文中使用委托方法navigationcontroller:willshowviewcontroller:animated:do work的示例,但您可能希望在自己的控制器中添加自己的导航项,并且给定的选项将删除您在自己的控制器中设置的任何内容。以下是我成功使用的代码,用于使此选项正常工作:

    - (void)navigationController:(UINavigationController *)navigationController
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated {
    
        // Here we want to remove the 'Cancel' button, but only if we're showing
        // either of the ABPeoplePickerNavigationController's top two controllers
        if ([navigationController.viewControllers indexOfObject:viewController] <= 1) {
    
            viewController.navigationItem.rightBarButtonItem = nil;
        }
    }
    

    请注意,导航控制器堆栈中有两个视图控制器,一个用于联系人组,另一个用于联系人列表。这就是为什么我们不能只检查FI视图控制器是导航控制器的顶视图控制器。

        3
  •  0
  •   Adam Eberbach Adil Shaikh    15 年前

    对此没有答案-如果您不能接受取消操作,请编写一个新的人员选取器。

        4
  •  0
  •   Uby    14 年前

    您可以通过选取器子视图来实现这个结果。只是有点无聊…

        5
  •  0
  •   joelm    14 年前

    我还没有尝试过,但我认为uby是在说要遍历选择器的子视图,直到找到iskindofclass:[uibarbuttonitem class]的子视图,然后您可以更改它的title属性。它也可能在导航栏的“item”数组中。

        6
  •  0
  •   brainimus user417509    13 年前

    将委托设置为PeoplePickerController控制器。 在委托类中,使用此委托方法。

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
     UIView *pCustomView = [[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)];
     UIBarButtonItem *pBtn = [[UIBarButtonItem alloc] initWithCustomView:pCustomView];
     [viewController.navigationItem setRightBarButtonItem:pBtn animated:NO];
     [pBtn release];
     [pCustomView release];
    }
    
        7
  •  0
  •   Vin    13 年前

    确保将选择器对象的委托(不是PeoplePickerDelegate,只是委托)设置为实现以下方法的类:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)];
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom];
    [viewController.navigationItem setRightBarButtonItem:btn animated:NO];
    [btn release];
    [custom release];
    } 
    
        8
  •  0
  •   Georg Schölly Crazy Developer    12 年前

    它工作得很好,但在iOS 4中还有一件事。当我使用快速应用程序切换功能切换回我的应用程序时,取消按钮再次出现。

    方法

    - (void)navigationController:(UINavigationController *)navigationController  
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    

    不会接到电话。所以我这样做:

    - (void)applicationDidEnterBackground:(UIApplication *)application {
        id topView = pickerControllerDelegate.peoplePicker.topViewController;
        topView.navigationItem.rightBarButtonItem = nil;
    }
    

    它工作得很好。

        9
  •  0
  •   Jens Erat    11 年前

    根据Russel B,你可以改写你的 可视检测器

    这对我很有用:

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        UINavigationItem *item = (UINavigationItem *)[self.navigationBar.items lastObject];
        item.rightBarButtonItems = [[NSArray alloc] init];
    
        item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
    }
    
        10
  •  0
  •   Russell B    11 年前

    编辑:参见下面的注释。这是一个不该做什么的例子。

    我试图通过对abpeoplePickernavigationController的子类化并截获所有更改当前导航视图控制器视图的事件,从而获得公共API所需的行为。然后就可以导航视图层次结构并清除所有不需要的按钮。

    您可以从委托导航视图层次结构,但不知道更改视图状态的事件…这使得取消按钮很难被取消并使其保持不变。

    本代码 有点 为我工作(注:暴力可以杀死所有的右手按钮):

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [self killCancelButton];
    }
    
    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        [super pushViewController:viewController animated:animated];
        [self killCancelButton];
    }
    
    - (UIViewController*)popViewControllerAnimated:(BOOL)animated {
        UIViewController *result = [super popViewControllerAnimated:animated];
        [self killCancelButton];
        return result;
    }
    
    - (void)killCancelButton {
        for (NSUInteger itemIdx = 0; itemIdx < self.navigationBar.items.count; itemIdx++) {
            UINavigationItem *item = [self.navigationBar.items objectAtIndex:itemIdx];
            item.rightBarButtonItems = [[NSArray alloc] init];
        }
    }