簡體   English   中英

ABPeoplePickerNavigationController-刪除“取消”按鈕而不使用私有方法/屬性?

[英]ABPeoplePickerNavigationController - remove “Cancel” button without using private methods/properties?

我使用的是ABPeoplePickerNavigationController,它是UINavigationController的子類,在上下文中,我使用的是右側的默認導航欄按鈕“取消”,這毫無意義。 我找不到禁用或隱藏它的方法,並且所使用的任何方法都必須是公開的並且可以存儲批准。 完全擺脫導航欄(picker.navigationBarHidden = YES;)可能是一個選擇,只是在彈出聯系人列表后,導航欄會重新出現。 子類化ABPeoplePickerNavigationController並攔截viewWillAppear嘗試取消取消按鈕無效。 [picker setAllowsCancel:NO]; 確實可以工作,但是沒有文件證明,所以我希望永遠不會通過批准。

這個

- (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是導航控制器的頂視圖控制器的原因。

編輯:請參閱下面的評論。 現在這是不執行操作的說明。

我試圖通過子類化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];
    }
}

根據羅素 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)];
}

對此沒有答案-如果您不能接受取消,請寫一個新的人員選擇器。

您可以通過選擇器子視圖瀏覽來實現該結果。 只是有點無聊...

將委托設置為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屬性。 它也可能在navigationBar的“ Item”數組中。

確保將選擇器對象的委托(不是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;
}

效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM