代码之家  ›  专栏  ›  技术社区  ›  Idan

如何使用editButtonItem初始化按钮

  •  0
  • Idan  · 技术社区  · 16 年前

    我有一个表视图,在这个表视图中我设置了一个节标题,我想包含一个编辑按钮。 我知道,如果我的viewcontroller是导航链的一部分,那么它有一个内置的支持,但是有没有办法不用它,或者唯一的选择是手动设置一个按钮并触发一个将替换标题和编辑属性的操作? 顺便说一句,我知道我在下面写的代码是错误的,只是想知道是否有一种方便的方法将uiBarButtonim转换为uiButtonim。谢谢

    -(UIView*) headerView
    {
        if (headerView)
            return headerView;
    
        float w = [[UIScreen mainScreen] bounds].size.width;
        CGRect headerViewFrame = CGRectMake(0,0, w,48);
        headerView = [[UIView alloc] initWithFrame:headerViewFrame];
    
        [headerView addSubview:[self editButtonItem]]; // I want to do something like that
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Chris    15 年前

    无法将UIBarbuttonite转换为UIButton。如果你查看他们的每个超类,你会发现uiBarButtonim继承自UIBarItem,而UIBarItem继承自NSObject(与UIView中的UIButton完全无关,它符合UIResponder协议)。

    对于此特定问题,可以覆盖以下方法:

    - (void) setEditing:(BOOL)editing animated:(BOOL)animated {
        [super setEditing: editing animated: animated];
        [self.tableView setEditing:editing animated:animated];
    }
    

    并有一个按钮(可以将其用作节标题视图)触发一个设置编辑状态的方法