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];
}
}
Related
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];
}
}
I want to remove a UITextField/UIButton/UILabel added to a view on the click of button. How can this be achieved in iOS?
To remove you can simply use:
[myTextField removeFromSuperview];
try this one...
Blockquote
UILabel *t1;
NSArray *arr1=[YourView subviews];
for (UIView *v in arr1)
{
if([v isKindOfClass:[UILabel class]])
{
t1 = (UILabel *)v;
if (t1.tag==your_tag)
[t1 removeFromSuperview];
}
}
Blockquote
You can use [myTextField removeFromSuperview]; to get rid of a view.
[myTextField removeFromSuperview]
See UIView documentation.
All controls inherited from UIView.
So you can remove all the controls which added on UIView using like this. By clicking the button action write like this
for(UIView *control in [self.view subviews])
{
[control removeFromSuperview];
}
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!
I am currently at topViewController while a UIView is added to my rootViewController. Is there any way that I can remove all UIViews before going to rootViewController via popToRootViewContoller?
In Going back action,type following code:
for (UIView *subview in self.view.subviews) {
[subview removeFromSuperview];
}
And then type popToRootViewContoller.
Add this in viewWillAppear: method of YourViewController
for (UIView *view in self.view.subviews) {
[view removeFromSuperview];
}
Hope it helps you
In viewWillDisappear you can do this task, when you are leaving your rootViewController then this method will call , so you can remove your view at this point.
-(void)viewWillDisappear:(BOOL)animated
{
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
Try this:
-(void)viewWillAppear:(BOOL)animated
{
NSArray *subviews = [self.view subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
}
[yourView removeFromSuperView]
but added subviews willnot be there when you pop a UIViewController, so i don't think you need to do this
- (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