Haseeb
Haseeb

Reputation: 361

How to remove UIButton in iOS7

I want to create and remove uibutton. (iOS7)

My viewDidLoad function creates uibutton in the following way:

for (char a = 'A'; a <= 'Z'; a++)
{
    position = position+10;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(getByCharacter:)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:[NSString stringWithFormat:@"%c", a] forState:UIControlStateNormal];
    button.frame = CGRectMake(position, self.view.frame.size.height-40, 10.0, 10.0);
    button.tintColor = [UIColor whiteColor];
    [self.view addSubview:button];

}

Now I want to remove specific uibutton based on button text.

- (IBAction)getByCharacter:(id)sender{
 }

I don't know how to do this. Can anyone provide some sample code?

Upvotes: 0

Views: 132

Answers (2)

iEngineer
iEngineer

Reputation: 1319

First you need to get the button's title text and then remove it from superView if it matches to the specific character,

- (IBAction)getByCharacter:(id)sender{

  UIButton *myButton = (UIButton *) sender;
  if([myButton.titleLabel.text isEqualToString:@"C"]{
    [myButton removeFromSuperView];
  }
}

Upvotes: 1

Loadex
Loadex

Reputation: 1512

You can remove the button by calling

[sender removeFromSuperview];

in your callback (getByCharacter:). When you attach the selector to the button, the sender parameter will be the button which triggered the event. That means that you have an instance of it. With this instance you can now calll the removeFromSuperview method.

Upvotes: 4

Related Questions