Looping through specific class in view : Objective-c - iphone

I have been using the following code to loop through specific classes in my subviews.
for (int i = 0; i < [[self.view subviews] count]; i++) {
if ([[self.view.subviews objectAtIndex:i] class] == [UIButton class]) {
}
}
But i feel like there should be a better way. Could someone please help me out?
Thanks.

for(UIView *v in [self.view subviews]) {
if ([v isKindOfClass:[UIButton class]]) {
...
}
}

You can also try this to iterate through a specific class in a view. I find it a bit cleaner.
for (UIButton *button in [self.view subviews]) {
// do whatever e.g. button.layer.cornerRadius = 11.0;
}

Use this instead:
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UIButton class]]) {
//...
}
}
Alternately, you could use a block by sending the subviews array -enumerateObjectsUsingBlock:.

Related

How to remove all buttons from a view in iOS 7?

How can I remove all buttons from a view using iOS 7?
Here's code that works in earlier versions of iOS:
for(UIView *view in cell.subviews){
if([view isMemberOfClass:[UIButton class]]){
[(UIButton *)view removeFromSuperview];
}
}
First you need to get all subviews from view and then check all view is type of UIButton.For more info see this...
for (UIView *view in self.view.subviews)
{
if ([view isMemberOfClass:[UIButton class]])
{
[(UIButton *)view removeFromSuperview];
}
}
It looks like you're simply not doing a loop through the subviews. Assuming this is a view controller:
NSArray * allSubviews = [self.view subviews];
for(UIView view in allSubviews)
{
if([view isMemberOfClass:[UIButton class]])
{
[view removeFromSuperview];
}
}
You also don't need to cast "view" to "UIButton *" here because the base class of "UIView" is what implements "removeFromSuperview".
Sorry for late reply. Actually this part of code will work until iOS 6.0, but in iOS 7.0 and onwards, the code is not working.
So finally I found the solution to remove all subview from UIScrollview with the following statement.
[scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)].
Try, this will work
for(UIView *view in self.view.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
[view removeFromSuperview];
}
}

Access UIView inside UIScrollView correctly

I've this code:
int i=0;
for (UIView *view in [self subviews]) {
//NSLog(#"index %d - i %d",index,i);
if (i==index) {
NSLog(#"index %d - i %d",index,i);
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
i++;
}
"index" is a variable used to select the view to modify.
The problem is that nothing happen, instead if I run this code:
for (UIView *view in [self subviews]) {
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
every view will be modify obviously, but i need the first code :)
ideas?
Set the tag of all the subviews inside your scroll view and then pass the tag of selected view to the index variable hope it is an integer.
for(int i = 1; i <=4; i++){
UIView *myView = [UIView......]; // set your view
myView.tag = i;
[your_scrollView addSubview:myView];
}
then you can do like following.
for (UIView *view in [your_scrollView subviews])
{
if (view.tag == index)
{
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
}
or there is an easy way to get the view [your_scrollView viewWithTag:index]
Try this code :-
for (UIView *view in [scrollView subviews]) {
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
Hope it works

How to manage subviews added in a UIView?

I have a problem in clearing my view, For a particular case I have been adding user names as label into my view, I may have multiple user based on situation, now my problem is, For one case I want to clear the view without popping it, I am not sure how to remove those added labels, I have an idea I can set tag for each label, and I can clear using that later. Any other efficient way is there in this particular case.
Hope my question is clear thanks.
use
for (UIView* view in self.view.subviews) {
if(view isKindOfClass:[UILabel class]) {
//do whatever you want to do
}
}
You can remove all subviews like this:
for (UIView *subView in [view subviews])
[subView removeFromSuperview];
Or if you want to access a particular view with tag value of n,
UIView *subview = [view viewWithTag:n]
You can do in the following way
for (UIView *view in [self.view subviews])
{
if ([view isKindOfClass:[UILabel class]])
{
[view removeFromSuperview];
}
}
Hope you are asking about this.
[labelName removeFromSuperview];
use this :
for(id viewSub in self.view.subviews)
{
[viewSub removeFromSuperview];
}
this will remove all subviews of View
Try this:
for (UIView *v in [self.relatedView subviews])
{
[v removeFromSuperview];
}
Call below method and pass the "UIWebView" object as argument:
+(void)removeAllSubViews:(id)pObj
{
NSArray *Array = [pObj subviews];
for(int index = 0; index < [Array count]; index++)
{
[[Array objectAtIndex:index] removeFromSuperview];
}
}
You can also check object like this if you want for any particular object:
if(view isKindOfClass:[UILabel class]) {
//do whatever you want to do
}
Cheers!

After calling removeFromSuperview "interfacebuilded" objects are gone?

I have a normal Interace in interface builder (there's a UILabel and a UIButton). In code, I create a UIScrollView with a (unknown) number of UILabels (depends by the User). These labels are created in - (void)viewWillAppear to make sure the data is up to date. To remove the labels, in viewWillDisappear i'm calling
for(UIView *subview in [scrollView subviews])
{
[subview removeFromSuperview];
}
The problem is, after the view get's called again, the Objects created with interfacebuilder (the UILabel & UIButton) disappear. Do the get removed by calling [subview removeFromSuperview];? If yes, how to add them again? Everything which is created by code is still thereā€¦
you should add tag to your labels that you add dynamically.
for(int i = 0; i < n; i++){
UILabel *label = [UILabel alloc...];
label.tag = 999999;
}
then.
for(UIView *subview in [scrollView subviews])
{
if([subview isKindOfClass:[UILabel class]] && subview.tag == 999999){
[subview removeFromSuperview];
}
}
Don't remove all subviews. Remove only the labels you added in viewWillAppear.
UILabel, UIButton and UITextfields are basically subclasses of UIView.
So if you remove all the UIViews under the scrollview, it will also remove them too.
for (UIView *myView in [scrollView subviews]) {
if([myView isKindOfClass:[UILabel class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UIButton class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UITextField class]]){
[myView removeFromSuperview];
}
}

removeFromSuperview removes all subviews

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"will rotation");
for (UIButton *button in self.view.subviews) {
[button removeFromSuperview];
}
}
I have a problem with this code. I need to remove only UIButtons from my view. But this code also remove all subviews of my self.view. How can I solve this?
Do this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"will rotation");
for (id subview in self.view.subviews) {
if([subview isKindOfClass:[UIButton class]]) //remove only buttons
{
[subview removeFromSuperview];
}
}
}
You are iterating all the views and casting them to UIButton, not iterating UIButtons.
Try this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"will rotation");
for (UIView *button in self.view.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
[button removeFromSuperview];
}
}
}
for (id button in self.view.subviews)
{
if(button isKindOfClass:[UIButton class])
{
[button removeFromSuperview];
}
}
self.view.subviews will fetch all subviews, and using the type UIButton * in that way does not filter the list, it just hints to the compiler that you expect to be able to treat the objects like UIButtons. You must inspect each one, like this:
for (UIView *subview in self.view.subviews) {
if([subview isKinfOfClass:[UIButton class]])
[subview removeFromSuperview];
}
One option is to create a tag at the top of the file
#define BUTTONSTODELETE 123
and then set each of the buttons.tag = BUTTONSTODELETE
Finally :
for (UIButton *v in [self.view subviews]){
if (v.tag == BUTTONSTODELETE) {
[v removeFromSuperview];
Guarantees only items with that tag will be removed
EDIT: Maulik and fbernardo have a better, less messy way