2

I currently have a barbutton:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneDate:)];

It calls the following action:

- (IBAction)doneDate:(id)sender{
[self removeDateView]
}

Which calls the following method:

- (void)removeDateView{

NSLog(@"subviews of view3.view: %@",self.View3.subviews);
[self.View3.subviews. makeObjectsPerformSelector: @selector(removeFromSuperview)];

}

The subview that I'm trying to remove is

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];

At the moment it just deletes everything within that View, I can't seem to remove the view called containerView which has the datepicker and toolbar.

1
  • set tag for the view that contains date picker and toolbar, then get this view using viewWithTag method with the tag you specified. Then remove it from super view. Commented Apr 18, 2014 at 20:59

2 Answers 2

3

As erhnby stated, you could use a tag - which is a great method, but I always try to shy away from looping through a view's subviews whenever I can. Personally, I would make the view you are going to remove an instance variable, and when you want to remove it you can call remove directly on it... Just made a simple example that does this:

.h file:

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController {
    UIView *_containerView;
}

@end

.m file:

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (id)init {
    self = [super init];

    // create the bar button and set it as the right bar button on the navigation bar
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(removeDoneDate)];

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // create the container view and add it as a subview
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 100, 100)];
    _containerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_containerView];

}

- (void)removeDoneDate {
    // remove it
    [_containerView removeFromSuperview];
}

@end

Results in this to start:

enter image description here

Press button...

enter image description here

(sorry, didn't realize the white on white would be that hard to see)

Sign up to request clarification or add additional context in comments.

10 Comments

Ah I see, you've made it very easy to understand, thank you. Hopefully I'll implement it correctly.
Let me know if you need any help!
The subview's now being registered in NSLog now that I've added [self.view addSubview:_containerView]; but it's loading within viewDidLoad rather than as a subview when the textfield's input view is pressed
What are you NSLogging?
You added _containerView to self.'view', but you're printing subviews of self.'View3'. Could that be it?
|
-1

set tag for that will remove view

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];
[containerView setTag:100];

and find it and removeFromSuperView

for (UIView*  view in self.View3.subviews) {
        if ([view isKindOfClass:[UIView class]] && view.tag == 100) {
            [view removeFromSuperview];
        }
    }

5 Comments

It doesn't appear to work, the done button can be pressed but nothing is happening. NSLog(@"subviews of view3.view: %@",self.View3.subviews); I think the problem is that .subviews only returns 'immediate' views, which i'm assuming are ones that are already on my storyboard. Which i think is the problem as my view is programmatically created and not immediate?
self.view3 has subview ?
The NSLog says View3.subviews returns - <UINavigationBar> <UILabel> and a <UIView>
They are all correct for objects that are on my storyboard, but are not correct for the view that I added programmatically
What is wrong with using viewWithTag:. Why would you iterate through the subviews?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.