xcode iOS 上带有 Storyboard 的 TableView 导航栏中的返回、编辑和添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8416816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Back, Edit and Add buttons in navigation bar of TableView with Storyboard on iOS
提问by yassassin
I'm facing some problems in implementing a tableview, with "Back", "Edit" and "Add" buttons on the navigation bar. The tableview is reached by clicking on a row of another tableview, so the "Back" button is added automatically. With the storyboard I've added the "Add" button to the navigation bar. With code I've added the "Edit" button (I used code, since if I add the button with the storyboard, I don't know how to reproduce the "Edit" standard behavior...):
我在实现 tableview 时遇到了一些问题,导航栏上有“返回”、“编辑”和“添加”按钮。通过单击另一个 tableview 的一行到达 tableview,因此会自动添加“后退”按钮。通过情节提要,我在导航栏中添加了“添加”按钮。使用代码我添加了“编辑”按钮(我使用了代码,因为如果我添加带有情节提要的按钮,我不知道如何重现“编辑”标准行为......):
self.navigationItem.leftBarButtonItem = self.editButtonItem;
The problem is that, in this way, the "Edit" button hides the "Back" button on the navigation bar.
问题在于,通过这种方式,“编辑”按钮隐藏了导航栏上的“返回”按钮。
At this point, I've two questions:
在这一点上,我有两个问题:
- Is it possible with storyboard to add a third button on the navigation bar?
In case I've to do this programmatically, I know that I can do this as follows:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(width-90,6,50,30)]; [button setTitle:@"Edit" forState:UIControlStateNormal]; button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; [self.navigationController.navigationBar addSubview:button];
- 故事板是否可以在导航栏上添加第三个按钮?
如果我必须以编程方式执行此操作,我知道我可以按如下方式执行此操作:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(width-90,6,50,30)]; [button setTitle:@"Edit" forState:UIControlStateNormal]; button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; [self.navigationController.navigationBar addSubview:button];
But how can I implement via code the standard behavior of the "Edit" button? I mean, I click "Edit" and the button becomes "Done" and the rows become deletable...
但是如何通过代码实现“编辑”按钮的标准行为?我的意思是,我单击“编辑”,按钮变为“完成”,行变为可删除...
Thanks in advance, yassa
提前致谢,亚萨
采纳答案by smparkes
First, the Apple docs say 'you do not add subviews to a navigation bar directly'. Don't know if this is enough to get the app bounced from the store, but it's not considered "proper".
首先,Apple 文档说“您不直接将子视图添加到导航栏”。不知道这是否足以让应用程序从商店退回,但这不被认为是“正确的”。
Second, you can add more than three buttons to a UINavigationItem
in iOS 5 but not in iOS 4 or earlier.
其次,您可以UINavigationItem
在 iOS 5 中向 a 添加三个以上的按钮,但不能在 iOS 4 或更早版本中添加。
Finally, I'd leave the edit button top right and back top left. That's where people expect them. If I wanted an add button (and are on iOS 5), I'd place it next to the edit button.
最后,我将编辑按钮留在右上角和左上角。这就是人们期待他们的地方。如果我想要一个添加按钮(并且在 iOS 5 上),我会将它放在编辑按钮旁边。
Sorry; no help on storyboards. Don't know anything about them.
对不起; 对故事板没有帮助。对他们一无所知。
回答by CJPresler
Incase anyone else should happen to stumble onto this question as well the solution is pretty easy. UINavigationItem has a property for rightItems wich is just an array of UIBarButtonItems. Put both an add button and an edit button into an array and assign it to rightItems and your done :-) And here is an example code snippet:
以防万一其他人碰巧遇到这个问题,解决方案也很简单。UINavigationItem 有一个 rightItems 属性,它只是一个 UIBarButtonItems 数组。将一个添加按钮和一个编辑按钮放入一个数组中,并将其分配给 rightItems 和你完成的 :-) 这是一个示例代码片段:
UITableViewController *table = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
NSArray *barButtons = [NSArray arrayWithObjects:table.editButtonItem, addButton, nil];
table.navigationItem.rightBarButtonItems = barButtons;