如何隐藏导航栏中两个右键之一

时间:2010-09-14 20:22:32

标签: iphone objective-c cocoa-touch ios4

亲爱的,我已经在文本视图顶部右侧导航栏中实现了两个按钮;

UIToolbar* toolbar = [[UIToolbar alloc]
                      initWithFrame:CGRectMake(0, 0, 112, 44.5)];

// toolbar style is the default style

// create an array for the buttons

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a button to run the job
UIBarButtonItem *runButton = [[UIBarButtonItem alloc]
                              initWithTitle:@"RUN"
                              style:UIBarButtonItemStyleBordered
                              target:self
                              action:@selector(runAs:)];
// Button style is the default style
[buttons addObject:runButton];
[runButton release];

// create a spacer between the buttons

UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                           target:nil
                           action:nil];
[buttons addObject:spacer];
[spacer release];

// create a standard Edit/Done button with custom titles Edit/Save

self.editButtonItem.possibleTitles = [NSSet setWithObjects:@"Edit", @"Save", nil];
self.editButtonItem.title = @"Edit";
UIBarButtonItem *editButton = self.editButtonItem;
[buttons addObject:editButton];
[editButton release];

// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:YES];
[buttons release];

// place the toolbar into the navigation bar as Right Button item
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                          initWithCustomView:toolbar];
[toolbar release];

现在处于编辑模式我想隐藏RUN按钮,当RUN按钮运行时,我希望隐藏编辑按钮。有人可以建议我这样做而无需在编辑模式下重新定义按钮(就像后面/左侧按钮项setHidesBackButton:(BOOL)动画:(BOOL))或任何替代方法?非常感谢。

3 个答案:

答案 0 :(得分:2)

试试这个:

UIToolbar *toolbar = (UIToolbar *) self.navigationItem.rightBarButtonItem.customView;
UIBarButtonItem *runButton = (UIBarButtonItem *) [toolbar.items objectAtIndex: 0];
runButton.customView.hidden = YES;

答案 1 :(得分:0)

我需要专门显示/隐藏两个按钮中的一个。我在NSMutableArray上创建了一个类别,如果它们在数组中,则删除这两个按钮,然后在必要时添加每个按钮。您可能需要将按钮作为ViewController的属性。

<强>的NSMutableArray +扩展程序

@implementation NSMutableArray (Extensions)
-(bool)removeItemIfExists:(id)item {
    bool wasRemoved=false;
    for (int i=self.count-1;i>0;i--) {
        if([self objectAtIndex:i] == item){
            [self removeObjectAtIndex:i];
            wasRemoved = true;
        }
    }
    return wasRemoved;
}
@end

使用以下方式调用:

NSMutableArray *newLeftItems = [self.navigationItem.leftBarButtonItems mutableCopy];
[newLeftItems removeItemIfExists:btnOne];
[newLeftItems removeItemIfExists:btnTwo];
if(someCondition) { 
    [newLeftItems insertObject:btnOne atIndex:1];
} else {
    [newLeftItems insertObject:btnTwo atIndex:1];
}
[self.navigationItem setLeftBarButtonItems:newLeftItems animated:true];

答案 2 :(得分:0)

试试这个..

-(void) changeBarButtonVisibility:(UIBarButtonItem*) barButtonItem visibility:(BOOL) shouldShow {
UIColor *tintColor = shouldShow == NO ? [UIColor clearColor] : nil;
[barButtonItem setEnabled:shouldShow];
[barButtonItem setTintColor:tintColor];

}

并调用上述方法并传递要隐藏的栏按钮

[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[0] visibility:NO];
[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[1] visibility:YES];