Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count subviews in Super View

I have one class in which i have two method _addView() and _removeView().

And these method i am using in another class for adding view and removing view but its not working.If i am using in the same then its working.

For Remove-

- (id)deleteBoxAtIndex:(int)boxIndex{      
    for (int i = 0; i < [[self subviews] count]; i++ ) {
        [[[self subviews] objectAtIndex:boxIndex] removeFromSuperview];
    }

    return self;
}

Then how i count the subviews in that class or remove from that class.

like image 305
iPhonedev Avatar asked Jan 26 '26 06:01

iPhonedev


2 Answers

You correct in trying to use [self.subviews count] to count the number of subviews, but there is an elegant way to remove all subviews from a view in Objective-C. Try this:

[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperView)];
like image 50
Moshe Avatar answered Jan 28 '26 00:01

Moshe


You should pass a pointer to the UIView instance (the one that has got the subviews) to the other object so that you can call:

 myView.subviews

I don't know if this could work for you:

- (id)deleteBoxAtIndex:(int)boxIndex fromView:(UIView*)view {      
  for (int i = 0; i < [[view subviews] count]; i++ ) {
    [[[view subviews] objectAtIndex:boxIndex] removeFromSuperview];
  }

  return self;
}

If you give more detail about the relationship between the two classes you mention (basically, the names and how one interact with the other), I could give you more hints.

like image 35
sergio Avatar answered Jan 27 '26 22:01

sergio