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] objectA开发者_运维技巧tIndex:boxIndex] removeFromSuperview];
}
return self;
}
Then how i count the subviews in that class or remove from that class.
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)];
You should pass a pointer to the UIView
instance (the one that has got the subview
s) 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.
Your problem not in retrieving the count but in fact that your relay on a count that changes dynamically inside of your for loop logic execution. Do cleanup of subviews in the following way:
while([[self subviews] count] > 0) {
UIView *view = [[self subviews] objectAtIndex:0];
[view removeFromSuperview];
}
You can use the superview property:
[[self superview] subviews]
to do the similar loop you are doing right row. However I'd strongly encourage you to use
[[self superview] viewWithTag:boxIndex]
instead of objectAtIndex: method
Just do a loop of all subViews u want to remove:
for (UIView *subs in [self.view subviews]){
[subs removeFromSuperview];
}
精彩评论