2009-04-20 20 views
11

私のツールにはツールバーがあり、特定の時点でいくつかのボタンを無効または有効にしたいと考えています。これを行う最も簡単な方法は何ですか? UIToolbarのアイテムプロパティにどのようにアクセスできますか?ここでUIToolBar - 無効化ボタン

は私のコードです:

-(void)addToolbar { 
    NSMutableArray *buttons = [[NSMutableArray alloc] init]; 

    //Define space 
    UIBarButtonItem *flexibleSpaceItem; 
    flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]; 

    //Add "new" button 
    UIBarButtonItem *newButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"New" style:UIBarButtonItemStyleBordered target:self action:@selector(new_clicked)]; 
    [buttons addObject:newButton]; 
    [newButton release]; 

    //Add space 
    [buttons addObject:flexibleSpaceItem]; 

    //Add "make active" button 
    UIBarButtonItem *activeButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Make Active" style:UIBarButtonItemStyleBordered target:self action:@selector(make_active_clicked)]; 
    [buttons addObject:activeButton]; 
    [activeButton release]; 

    [buttons addObject:flexibleSpaceItem]; 

    //Add "edit" button 
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(edit_clicked)]; 
    [buttons addObject:editButton]; 
    [editButton release]; 

    [flexibleSpaceItem release]; 

    [toolBar setItems:buttons]; 
    [buttons release]; 
} 

は、事前にありがとうございます。

答えて

14

最も簡単な方法は、UIBarButtonItemへの参照をインスタンス変数として保存することです。

# header file 
UIBarButtonItem *editButton; 

次に、あなたのコードが

# .m file 
editButton = [[UIBarButtonItem alloc] 
       initWithTitle:@"Edit" 
       style:UIBarButtonItemStyleBordered 
       target:self 
       action:@selector(edit_clicked)]; 
[buttons addObject:editbutton]; 

は今どこにでも任意のインスタンスメソッドでは、ボタンを無効にするのと同じくらい簡単である次のようになります。

editButton.enabled = NO; 

は今もこのクラスするので、すぐにそれをreleaseいけませんボタンオブジェクトを所有しています。従ってreleaseそれは代わりにdeallocメソッドにあります。

+0

また、ツールバーのユーザー操作を無効にすることもできます(userInteractionEnbaled = NO) – jjxtra

6

Fast enumerationレスキュー!

- (void) setToolbarButtonsEnabled:(BOOL)enabled 
{ 
    for (UIBarButtonItem *item in self.toolbarItems) 
    { 
     item.enabled = !enabled; 
    } 
} 
関連する問題