1

UIViewController で、ツールバーに UIBarButtonItem を追加したいのですが、新しいボタンが表示されません。私は何を間違っていますか?

- (void)doLogin:(NSString *)name password:(NSString *)password {
 // 1.: start the Thread:
 NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(backgroundTaskLogin:) object:request];
 [self.opQueue addOperation:invOperation];
}

- (void)backgroundTaskLogin:(NSString *)request2 {
 // 2.: jump back in the Main Thread in show a cancel button in den toolbar:
 [self performSelectorOnMainThread:@selector(showCancelButton) withObject:nil waitUntilDone:NO];
}

- (void)showCancelButton {
 // 3.: add a new Cancel-Button in the Toolbar:
 UIBarButtonItem *tempButtonCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelLogin)];
 NSMutableArray *myButtons = (NSMutableArray *)self.toolbarItems;
 NSLog(@"Count buttons: %d", [self.toolbarItems count]); // DEBUGGER: 2

 [myButtons addObject:tempButtonCancel];
 [tempButtonCancel release];

 NSLog(@"Count buttons: %d", [self.toolbarItems count]); // DEBUGGER: 3

 // PROBLEM: I don't see the new Toolbar-Button :-(
}
4

2 に答える 2

2

self.toolbarItems可変配列であることに依存することはできません。以前にそのプロパティに変更可能な配列を割り当てた場合、それはあなたのケースの 1 つかもしれませんが、文書化されたインターフェイスを使用しない場合、View Controller がプロパティの変更に気付くことは期待できません。

新しい配列を作成し、setter を使用して次のように割り当てますtoolbarItems

NSMutableArray *newToolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[newToolbarItems addObject:tempButtonCancel];
self.toolbarItems = newToolbarItems;
于 2010-11-16T15:30:47.820 に答える
0

Ole の投稿のコードはあなたのバグを修正しますが、彼が示唆する理由ではありません (ほとんどの場合、それが変更可能であってはならない場合でも、配列に項目を正常に追加しているように見えるため) setToolbarItems:UIToolbar は、それ以外の場合、配列に変更が加えられたことを検出しないため、items 配列を複製して変更した後に呼び出す必要があります。

setToolbarItems:animated:物事をうまくフェードインするために使用することもできます;-)

于 2010-11-16T15:38:26.947 に答える