私は UINavigationController のサブクラスである ABPeoplePickerNavigationController を使用しています。コンテキストでは、右側のデフォルトのナビゲーション バー ボタン「キャンセル」を使用していますが、意味がありません。無効化または非表示にする方法が見つかりません。使用する方法は、公開され、ストアが承認できる必要があります。ナビゲーション バーを完全に削除する (picker.navigationBarHidden = YES;) こともできますが、連絡先のリストに戻った後にナビゲーション バーが再表示される場合を除きます。ABPeoplePickerNavigationController をサブクラス化し、viewWillAppear をインターセプトして試行し、キャンセル ボタンを nil しても機能しませんでした。[ピッカー set AllowCancel:NO]; DOESは機能しますが、文書化されていないため、承認に合格することはないと思います.
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;
}
}
ナビゲーションコントローラーのスタックには、連絡先グループ用と連絡先リスト用の2つのビューコントローラーがあることに注意してください。これが、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];
}
}
これに対する答えはありません-キャンセルで生きられない場合は、新しい人のピッカーを書いてください。
それは正常に動作しますが、iOS 4 ではもう 1 つ問題があります。アプリの高速切り替え機能を使用してアプリに戻ると、キャンセル ボタンが再び表示されます。
方法
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
呼び出されません。だから私はこれを作った:
- (void)applicationDidEnterBackground:(UIApplication *)application {
id topView = pickerControllerDelegate.peoplePicker.topViewController;
topView.navigationItem.rightBarButtonItem = nil;
}
それはかなりうまくいきます。
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)];
}
ピッカーのサブビューを参照することで、その結果を得ることができます。ちょっと退屈…
ピッカー オブジェクトのデリゲート (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];
}
まだ試していませんが、isKindOfClass:[UIBarButtonItem class] が見つかるまでピッカーのサブビューを反復処理するように Uby が言っていると思います。その後、その title プロパティを変更できます。また、navigationBar の 'Item' 配列にある場合もあります。
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];
}