Curnelious Curnelious - 1 month ago 5
Objective-C Question

How to find UIButton by title

I am having a

that has button in it. The buttons are not in some array.

I would like to look at the whole
loop) and look for a button by its title, and later remove it.

- (void)removeButtonByTitle:(NSString*)name
for (buttons in view) {
// find the button with the name "name" and remove it from the view

I couldn't find a way to do that without saving their names/pointers to an array.

omz omz

It's generally preferable to use the tag property for this. Then you can simply find the button with the viewWithTag: method and don't have to adjust your code if you decide to change the button title or localize your app.

If you really need to find a button by its title, you could do it like this:

NSString *buttonTitle = @"name";
UIButton *buttonWithTitle = nil;
for (UIButton *button in view.subviews) {
    if (![button isKindOfClass:[UIButton class]]) continue;
    if ([[button currentTitle] isEqualToString:buttonTitle]) {
        buttonWithTitle = button;
//do something with the button...