0
我像這樣在導航欄中添加了多個按鈕。如何訪問以編程方式創建的按鈕?
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(assignSelected)];
bi.style = UIBarButtonItemStyleBordered;
[bi setEnabled:FALSE];
[buttons addObject:bi];
[buttons addObject:self.editButtonItem];
[tools setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.editButtonItem.title = @"Select";
本質上我正在這樣做,所以他們可以使用編輯按鈕來選擇一組項目,並對他們採取行動。我想啓用在他們正在編輯時添加在那裏的操作按鈕。我有以下方法來控制編輯時按鈕的說明。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
if (editing)
{
self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
}
else
{
self.editButtonItem.title = NSLocalizedString(@"Select", @"Select");
}
}
這就是我要啓用和禁用操作按鈕的位置。我不知道如何訪問該按鈕,因爲它是以編程方式創建的。
想着你的問題多一點後,我認識你很可能具有不同的方法訪問「雙向」的問題。在頭文件中設置UIBarButtonItem * bi,並將其等於零。 'UIBarButtonItem * bi = nil;'然後在你的實現文件中,離開類和指針,並從你的頭文件初始化變量。 'bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(assignSelected)];'然後類中的所有方法都可以訪問它。 – Kyle 2012-04-25 22:17:04
這樣一個明顯的答案,我應該想到這一點。 – Jhorra 2012-04-25 22:26:17