Idan
Idan

Reputation: 5817

How to initialize a button using editButtonItem

I have a table view , within that tableview I'm setting up a section header, which I want to contain an edit button. I know there is a built in support for something like that if my viewcontroller is part of a navigation chain, but is there a way to do it without it, or the only option is to set a button manually and trigger an action that will replace the title and the editing property? btw - I know the code I wrote below is wrong, just wondering if there is a convenient way to convert a UIBarButtonItem to a UIButton. Thanks

-(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
}

Upvotes: 0

Views: 577

Answers (1)

Chris
Chris

Reputation: 403

There is no way to convert a UIBarButtonItem into a UIButton. If you look at each of their superclasses you will find that UIBarButtonItem inherits from UIBarItem which inherits from NSObject (not related at all to UIButton which from UIView and conforms to the UIResponder protocol).

For this specific problem you can override the following method:

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

and have a button (you can use it as your section header view) trigger a method that sets the edit status

Upvotes: 1

Related Questions