I've been trying to learn the new Storyboard feature in Xcode and I've run into a problem with trying to set a UITableView to edit mode.
我一直在尝试在Xcode中学习新的Storyboard功能,并且在尝试将UITableView设置为编辑模式时遇到了问题。
So far my storyboard looks like this:
到目前为止,我的故事板看起来像这样:
NavigationController -> UIViewController (subclass with tableview property)
NavigationController - > UIViewController(具有tableview属性的子类)
I added a Navigation Item and a Bar Button item to the view controller scene, so I do see an edit button. It didn't do anything automagically, so I tried linking it's selector to the setEditing method of the tableview delegate. This did put it into editing mode. However, the edit button did not change to a "Done" button and so there is no way to get out of editing mode.
我在视图控制器场景中添加了导航项和条形按钮项,因此我看到了一个编辑按钮。它没有自动执行任何操作,因此我尝试将其选择器链接到tableview委托的setEditing方法。这确实使它进入了编辑模式。但是,编辑按钮没有更改为“完成”按钮,因此无法退出编辑模式。
Do I have to create another Navigation item for the Done button? How do I connect it so that it appears at the right time and works correctly?
我是否必须为完成按钮创建另一个导航项?如何连接它以便它在正确的时间出现并正常工作?
7 个解决方案
#1
13
I just started using Storyboards, so I also wanted to use the Storyboard to add my Edit button. It is annoying to have taken the time to learn how to use a new tool but find you need a roll of duct tape to patch up the holes.
我刚开始使用Storyboard,所以我也想使用Storyboard添加我的编辑按钮。花时间学习如何使用新工具但发现需要一卷胶带来修补孔是很烦人的。
You can get it to work, but need to add a Custom button. In the Attributes inspector make sure the Identifier is Custom and the title is Edit.
你可以让它工作,但需要添加一个自定义按钮。在“属性”检查器中,确保“标识符”为“自定义”,标题为“编辑”。
Then add something like this in your .m
然后在你的.m中添加这样的东西
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = @"Edit";
[super setEditing:NO animated:YES];
} else {
sender.title = @"Done";
[super setEditing:YES animated:YES];
}
}
Have your Custom Edit button call the setEditMode method.
让自定义编辑按钮调用setEditMode方法。
Can only hope they will fix the implementation of the Edit button in the Storyboard editor in the future.
只能希望他们将来在Storyboard编辑器中修复Edit按钮的实现。
#2
27
I think that also with Storyboard, the only way (for sure, the easiest one) to implement a working edit/done button, is to use the following code:
我认为,使用Storyboard,实现工作编辑/完成按钮的唯一方法(当然,最简单的方法)是使用以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
...
//set the edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
This is the solution that Apple itself implements if you select a "Master-Detail Application" template for your project.
如果您为项目选择“Master-Detail Application”模板,这就是Apple自己实施的解决方案。
Probably Storyboard is still not perfect, and hopefully it will be improved from Apple in next releases...
可能Storyboard仍然不完美,希望在下一版本中它将从Apple改进......
#3
6
To summarize:
总结一下:
The Button, returned by UIViewController.editButtonItem
is a special toggling button with special behavior that calls - (void)setEditing:(BOOL)editing animated:(BOOL)animated
if pressed.
由UIViewController.editButtonItem返回的Button是一个特殊的切换按钮,具有特殊的行为,调用 - (void)setEditing:(BOOL)编辑动画:(BOOL)如果按下动画。
The Button, returned by UINavigationController.editButtonItem
is a simple Button, just labeled with "Edit".
由UINavigationController.editButtonItem返回的Button是一个简单的Button,只标有“Edit”。
The Storyboard allows to select the latter one.
故事板允许选择后者。
#4
5
If you are using the navigation controller to push to the view controller, simply set self.navigationItem.rightBarButtonItem = self.editButtonItem;
, which will put the default Edit button in the right. If the navigation bar is not visible, call self.navigationController.navigationBarHidden = NO;
. Those would be called in the viewDidLoad
method, or something similar. Then in order to get the tableView to respond to the edit call, use the following method:
如果您使用导航控制器推送到视图控制器,只需设置self.navigationItem.rightBarButtonItem = self.editButtonItem;,它将默认的编辑按钮放在右侧。如果导航栏不可见,请调用self.navigationController.navigationBarHidden = NO;。那些将在viewDidLoad方法中调用,或类似的东西。然后,为了使tableView响应编辑调用,请使用以下方法:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
}
That should do what you want it to do. If you have any issues, just say so and we can narrow down the details
那应该做你想做的事。如果您有任何问题,请说明一下,我们可以缩小细节范围
#5
2
To add to @Graham answer, you might also want to change the style so you can have the "Done" button style (the blue color). Something like this:
要添加到@Graham答案,您可能还需要更改样式,以便可以使用“完成”按钮样式(蓝色)。像这样的东西:
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = @"Edit";
sender.style = UIBarButtonItemStylePlain;
[super setEditing:NO animated:YES];
} else {
sender.title = @"Done";
sender.style = UIBarButtonItemStyleDone;
[super setEditing:YES animated:YES];
}
}
#6
1
one can use the dumb, not working Edit button from the Storyboard editor and then programmatically replace it with the UIViewController.editButtonItem
.
可以使用Storyboard编辑器中的哑,不工作的Edit按钮,然后以编程方式将其替换为UIViewController.editButtonItem。
in viewDidLoad:
在viewDidLoad中:
NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[toolbarItems replaceObjectAtIndex:0 withObject:self.editButtonItem];
[self setToolbarItems:toolbarItems];
this code assumes one has added the dumb Edit button as the leftmost item on the toolbar in the Storyboard.
此代码假定有人将“哑编辑”按钮添加为故事板中工具栏上最左侧的项目。
#7
1
In case that you have UIViewController and inside this you added a UITableVIew. If you want to add an edit UIBarButton in order to interact with UITableView, try:
如果您有UIViewController并在其中添加了UITableVIew。如果要添加编辑UIBarButton以便与UITableView交互,请尝试:
Add this line...
添加此行...
- (void)viewDidLoad
{
[super viewDidLoad];
...
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
}
and this method
而这种方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.myListTableView setEditing:editing animated:animated];
if(self.myListTableView.editing) {
NSLog(@"editMode on");
} else {
NSLog(@"editMode off");
}
}
where
哪里
@property (weak, nonatomic) IBOutlet UITableView *myListTableView;
#1
13
I just started using Storyboards, so I also wanted to use the Storyboard to add my Edit button. It is annoying to have taken the time to learn how to use a new tool but find you need a roll of duct tape to patch up the holes.
我刚开始使用Storyboard,所以我也想使用Storyboard添加我的编辑按钮。花时间学习如何使用新工具但发现需要一卷胶带来修补孔是很烦人的。
You can get it to work, but need to add a Custom button. In the Attributes inspector make sure the Identifier is Custom and the title is Edit.
你可以让它工作,但需要添加一个自定义按钮。在“属性”检查器中,确保“标识符”为“自定义”,标题为“编辑”。
Then add something like this in your .m
然后在你的.m中添加这样的东西
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = @"Edit";
[super setEditing:NO animated:YES];
} else {
sender.title = @"Done";
[super setEditing:YES animated:YES];
}
}
Have your Custom Edit button call the setEditMode method.
让自定义编辑按钮调用setEditMode方法。
Can only hope they will fix the implementation of the Edit button in the Storyboard editor in the future.
只能希望他们将来在Storyboard编辑器中修复Edit按钮的实现。
#2
27
I think that also with Storyboard, the only way (for sure, the easiest one) to implement a working edit/done button, is to use the following code:
我认为,使用Storyboard,实现工作编辑/完成按钮的唯一方法(当然,最简单的方法)是使用以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
...
//set the edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
This is the solution that Apple itself implements if you select a "Master-Detail Application" template for your project.
如果您为项目选择“Master-Detail Application”模板,这就是Apple自己实施的解决方案。
Probably Storyboard is still not perfect, and hopefully it will be improved from Apple in next releases...
可能Storyboard仍然不完美,希望在下一版本中它将从Apple改进......
#3
6
To summarize:
总结一下:
The Button, returned by UIViewController.editButtonItem
is a special toggling button with special behavior that calls - (void)setEditing:(BOOL)editing animated:(BOOL)animated
if pressed.
由UIViewController.editButtonItem返回的Button是一个特殊的切换按钮,具有特殊的行为,调用 - (void)setEditing:(BOOL)编辑动画:(BOOL)如果按下动画。
The Button, returned by UINavigationController.editButtonItem
is a simple Button, just labeled with "Edit".
由UINavigationController.editButtonItem返回的Button是一个简单的Button,只标有“Edit”。
The Storyboard allows to select the latter one.
故事板允许选择后者。
#4
5
If you are using the navigation controller to push to the view controller, simply set self.navigationItem.rightBarButtonItem = self.editButtonItem;
, which will put the default Edit button in the right. If the navigation bar is not visible, call self.navigationController.navigationBarHidden = NO;
. Those would be called in the viewDidLoad
method, or something similar. Then in order to get the tableView to respond to the edit call, use the following method:
如果您使用导航控制器推送到视图控制器,只需设置self.navigationItem.rightBarButtonItem = self.editButtonItem;,它将默认的编辑按钮放在右侧。如果导航栏不可见,请调用self.navigationController.navigationBarHidden = NO;。那些将在viewDidLoad方法中调用,或类似的东西。然后,为了使tableView响应编辑调用,请使用以下方法:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
}
That should do what you want it to do. If you have any issues, just say so and we can narrow down the details
那应该做你想做的事。如果您有任何问题,请说明一下,我们可以缩小细节范围
#5
2
To add to @Graham answer, you might also want to change the style so you can have the "Done" button style (the blue color). Something like this:
要添加到@Graham答案,您可能还需要更改样式,以便可以使用“完成”按钮样式(蓝色)。像这样的东西:
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = @"Edit";
sender.style = UIBarButtonItemStylePlain;
[super setEditing:NO animated:YES];
} else {
sender.title = @"Done";
sender.style = UIBarButtonItemStyleDone;
[super setEditing:YES animated:YES];
}
}
#6
1
one can use the dumb, not working Edit button from the Storyboard editor and then programmatically replace it with the UIViewController.editButtonItem
.
可以使用Storyboard编辑器中的哑,不工作的Edit按钮,然后以编程方式将其替换为UIViewController.editButtonItem。
in viewDidLoad:
在viewDidLoad中:
NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[toolbarItems replaceObjectAtIndex:0 withObject:self.editButtonItem];
[self setToolbarItems:toolbarItems];
this code assumes one has added the dumb Edit button as the leftmost item on the toolbar in the Storyboard.
此代码假定有人将“哑编辑”按钮添加为故事板中工具栏上最左侧的项目。
#7
1
In case that you have UIViewController and inside this you added a UITableVIew. If you want to add an edit UIBarButton in order to interact with UITableView, try:
如果您有UIViewController并在其中添加了UITableVIew。如果要添加编辑UIBarButton以便与UITableView交互,请尝试:
Add this line...
添加此行...
- (void)viewDidLoad
{
[super viewDidLoad];
...
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
}
and this method
而这种方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.myListTableView setEditing:editing animated:animated];
if(self.myListTableView.editing) {
NSLog(@"editMode on");
} else {
NSLog(@"editMode off");
}
}
where
哪里
@property (weak, nonatomic) IBOutlet UITableView *myListTableView;