【问题标题】:uitoolbar in navigation bar.导航栏中的uitoolbar。
【发布时间】:2011-11-29 02:13:34
【问题描述】:

这是一个简单的问题,但我是 xcode 开发的新手。我按照在线指南在导航栏上有多个按钮。导航栏上的编辑按钮有一个名为“editButton”的IBAction 方法。其中有一个(id)sender 作为参数。我是否得到发件人并将文本从编辑更改为完成,完成编辑?

"UIBarButtonitem *bbi = (UIBarButonItem *) sender;" 似乎不起作用。如何在导航栏的工具栏中获取按钮?

谢谢。

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];

// create a standard "EDIT" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButton:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];

// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

[buttons release];

// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];



-(IBAction)editButton:(id) sender{
UIBarButtonitem *bbi = (UIBarButonItem *) sender;

if (bbi title isequalsString:@"Done){
[bbi setTitle:@"Edit"];
}
}else{
[bbi setTitle:@"Done"];
}

}

【问题讨论】:

    标签: iphone xcode ios4


    【解决方案1】:

    问题在于您使用了UIBarButtonSystemItemEdit,而不是标准的uibarbutton。 尝试使用以下方法创建它:

    bi = [UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered  target:self action:@selector(editButton:)];
    

    然后按原样使用其余代码。

    【讨论】:

    • 完全按照你现在的方式 - 通过从发件人那里投射: UIBarButtonitem *bbi = (UIBarButonItem *) sender;
    【解决方案2】:

    UIBarButtonSystemItemEdit 是一个特殊的栏按钮项,它为您处理“编辑/完成”状态。无需手动更改按钮的文本。

    【讨论】:

    • 其实我说错了。如果您使用的是 UIViewController 子类,则应该能够使用内置的编辑按钮属性。 [self editButtonItem]
    猜你喜欢
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多