在iOS上带有情节提要的TableView的导航栏中返回、编辑和添加按钮

在iOS上带有情节提要的TableView的导航栏中返回、编辑和添加按钮,ios,xcode,button,uinavigationbar,edit,Ios,Xcode,Button,Uinavigationbar,Edit,我在实现tableview时遇到了一些问题,导航栏上有“后退”、“编辑”和“添加”按钮。 通过单击另一个tableview的一行可以访问tableview,因此“后退”按钮会自动添加。 在故事板中,我将“添加”按钮添加到导航栏。 在代码中,我添加了“编辑”按钮(我使用了代码,因为如果我在情节提要中添加按钮,我不知道如何重现“编辑”标准行为……: 问题在于,通过这种方式,“编辑”按钮隐藏了导航栏上的“后退”按钮 在这一点上,我有两个问题: 使用故事板是否可以在导航栏上添加第三个按钮 如果我必须以编

我在实现tableview时遇到了一些问题,导航栏上有“后退”、“编辑”和“添加”按钮。 通过单击另一个tableview的一行可以访问tableview,因此“后退”按钮会自动添加。 在故事板中,我将“添加”按钮添加到导航栏。 在代码中,我添加了“编辑”按钮(我使用了代码,因为如果我在情节提要中添加按钮,我不知道如何重现“编辑”标准行为……:

问题在于,通过这种方式,“编辑”按钮隐藏了导航栏上的“后退”按钮

在这一点上,我有两个问题:

  • 使用故事板是否可以在导航栏上添加第三个按钮
  • 如果我必须以编程方式执行此操作,我知道我可以按如下方式执行此操作:

    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];
    
  • 但是如何通过代码实现“编辑”按钮的标准行为呢?我的意思是,我点击“编辑”,按钮变为“完成”,行变为可删除

    提前感谢,,
    yassa

    首先,苹果的文档说“你不能直接将子视图添加到导航栏”。我不知道这是否足以让应用程序从商店中跳出,但这被认为是不“合适的”

    其次,在iOS 5中,您可以向
    UINavigationItem
    添加三个以上的按钮,但在iOS 4或更早版本中不能

    最后,我将保留编辑按钮的右上角和左上角。这是人们期待的。如果我想要一个添加按钮(并且在iOS5上),我会把它放在编辑按钮旁边


    对不起;在故事板上没有帮助。我对他们一无所知。

    万一其他人碰巧也碰到了这个问题,解决方法很简单。UINavigationItem具有rightItems的属性,它只是UIBarButtonims的一个数组。将“添加”按钮和“编辑”按钮放入数组,并将其分配给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;
    

    非常感谢你的回答。但要以编程方式实现带有3个按钮的工具栏,我是否必须创建一个新工具栏:
    UIToolbar*tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0,0,105,44.01)]
    或者我可以简单地将另一个按钮添加到导航栏
    [self-navigationItem]
    ?如果是,以哪种方式?您只需向导航项添加一个或多个按钮。请参见UINavigationItem()中名称中带有BarButton的属性和方法。通常,我会覆盖视图控制器中的
    navigationItem
    方法,调用
    super
    以获取默认值,如果尚未添加按钮,则添加按钮并返回结果。非常感谢,您为我打开了一个全新的世界!我能用三个按钮实现一个奇妙的导航栏!!!:)这确实是swift中更好的答案:
    let addButton=UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Add,target:self,action:“methodName:”);self.navigationItem.rightBarButtonItems=[self.editButtonItem(),addButton]
    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;