简体   繁体   中英

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?

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.

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];
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM