我正在使用 ABPeoplePickerNavigationController,它是 UINavigationController 的子类,在上下文中,我使用的是右侧的默认导航栏按钮“取消”,没有任何意义。我找不到禁用或隐藏它的方法,并且使用的任何方法都必须是公开的和商店认可的。完全摆脱导航栏(picker.navigationBarHidden = YES;)可能是一个选项,除了在弹出回到联系人列表后导航栏重新出现。继承 ABPeoplePickerNavigationController 并拦截 viewWillAppear 以尝试取消取消按钮无效。[选择器设置允许取消:否];确实有效,但没有记录,所以我希望永远不会通过批准。
10 回答
这个
- (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];
}
工作完美!
此处使用委托方法 navigationController:willShowViewController:animated: 的示例确实有效,但您可能希望在自己的控制器中添加自己的导航项,并且给定的选项将删除您可能在自己的控制器中设置的任何内容. 这是我已成功用于使此选项正常工作的代码:
- (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;
}
}
请注意,导航控制器的堆栈中有两个视图控制器,一个用于联系人组,一个用于联系人列表。这就是为什么我们不能只检查 viewController 是导航控制器的顶视图控制器的原因。
对此没有答案 - 如果您无法忍受取消,请编写一个新的人员选择器。
您可以通过选择器子视图浏览该结果。就是有点无聊……
将委托设置为 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];
}
我还没有尝试过,但我认为 Uby 说要遍历选择器的子视图,直到找到 isKindOfClass:[UIBarButtonItem class] 的子视图,然后您可以更改它的 title 属性。它也可能在导航栏的“项目”数组中。
确保将选取器对象的委托(不是 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];
}
它工作正常,但在 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;
}
它工作得很好。
编辑:见下面的评论。现在这是不做什么的说明。
我试图通过子类化 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];
}
}
根据 russel b 你可以覆盖你的viewdidapper
这对我有用:
- (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)];
}