How to remove subviews in Objective-C?
I have added UIButton and UITextView as subviews to my view programmatically.
notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];
textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[self.view addSubview:textView];
printf("n description button n");
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];
I need to remove all subviews when the button is clicked.
I have tried:
[self.view removeFromSuperView]
but it's not working.
objective-c subview
add a comment |
I have added UIButton and UITextView as subviews to my view programmatically.
notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];
textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[self.view addSubview:textView];
printf("n description button n");
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];
I need to remove all subviews when the button is clicked.
I have tried:
[self.view removeFromSuperView]
but it's not working.
objective-c subview
add a comment |
I have added UIButton and UITextView as subviews to my view programmatically.
notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];
textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[self.view addSubview:textView];
printf("n description button n");
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];
I need to remove all subviews when the button is clicked.
I have tried:
[self.view removeFromSuperView]
but it's not working.
objective-c subview
I have added UIButton and UITextView as subviews to my view programmatically.
notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];
textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[self.view addSubview:textView];
printf("n description button n");
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];
I need to remove all subviews when the button is clicked.
I have tried:
[self.view removeFromSuperView]
but it's not working.
objective-c subview
objective-c subview
edited Nov 12 '18 at 4:33
Cœur
17.5k9104145
17.5k9104145
asked Jun 9 '10 at 11:57
macmac
3,13072533
3,13072533
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I assume you're calling [self.view removeFromSuperView]
from a method in the same class as the above snippet.
In that case [self.view removeFromSuperView]
removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:
[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];
Perhaps you'd want to store those subviews in an NSArray
and loop over that array invoking removeFromSuperview
on each element in that array.
what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
– mac
Jun 9 '10 at 12:28
i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
– mac
Jun 9 '10 at 12:37
What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
– SanitLee
Sep 26 '16 at 1:23
add a comment |
to remove all the subviews you added to the view
use the following code
for (UIView *view in [self.view subviews])
[view removeFromSuperview];
add a comment |
I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)
Anyway, this is the little helper method that I use for that:
- (void)removeAllSubviewsFromUIView:(UIView *)parentView
for (id child in [parentView subviews])
if ([child isMemberOfClass:[UIView class]])
[child removeFromSuperview];
EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?
Am using that now as follows:
// Make sure the background and foreground views are empty:
[self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
I like that better.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3005540%2fhow-to-remove-subviews-in-objective-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I assume you're calling [self.view removeFromSuperView]
from a method in the same class as the above snippet.
In that case [self.view removeFromSuperView]
removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:
[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];
Perhaps you'd want to store those subviews in an NSArray
and loop over that array invoking removeFromSuperview
on each element in that array.
what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
– mac
Jun 9 '10 at 12:28
i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
– mac
Jun 9 '10 at 12:37
What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
– SanitLee
Sep 26 '16 at 1:23
add a comment |
I assume you're calling [self.view removeFromSuperView]
from a method in the same class as the above snippet.
In that case [self.view removeFromSuperView]
removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:
[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];
Perhaps you'd want to store those subviews in an NSArray
and loop over that array invoking removeFromSuperview
on each element in that array.
what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
– mac
Jun 9 '10 at 12:28
i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
– mac
Jun 9 '10 at 12:37
What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
– SanitLee
Sep 26 '16 at 1:23
add a comment |
I assume you're calling [self.view removeFromSuperView]
from a method in the same class as the above snippet.
In that case [self.view removeFromSuperView]
removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:
[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];
Perhaps you'd want to store those subviews in an NSArray
and loop over that array invoking removeFromSuperview
on each element in that array.
I assume you're calling [self.view removeFromSuperView]
from a method in the same class as the above snippet.
In that case [self.view removeFromSuperView]
removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:
[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];
Perhaps you'd want to store those subviews in an NSArray
and loop over that array invoking removeFromSuperview
on each element in that array.
edited Aug 21 '13 at 8:10
answered Jun 9 '10 at 12:05
Frank SheararFrank Shearar
15.1k65583
15.1k65583
what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
– mac
Jun 9 '10 at 12:28
i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
– mac
Jun 9 '10 at 12:37
What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
– SanitLee
Sep 26 '16 at 1:23
add a comment |
what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
– mac
Jun 9 '10 at 12:28
i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
– mac
Jun 9 '10 at 12:37
What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
– SanitLee
Sep 26 '16 at 1:23
what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
– mac
Jun 9 '10 at 12:28
what about the button, i have created , by adding as sub view. actually i am trying to show a view with textview,when button pressed , after when i click the another button that is in present view, it has to go back my previous view.
– mac
Jun 9 '10 at 12:28
i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
– mac
Jun 9 '10 at 12:37
i have tried [notesDescriptionView removeFromSuperview]; [textView removeFromSuperview]; but its not working.
– mac
Jun 9 '10 at 12:37
What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
– SanitLee
Sep 26 '16 at 1:23
What is the right way to call such a method that removes subview from superview from other class? I'm trying delegate method but it doesn't work.
– SanitLee
Sep 26 '16 at 1:23
add a comment |
to remove all the subviews you added to the view
use the following code
for (UIView *view in [self.view subviews])
[view removeFromSuperview];
add a comment |
to remove all the subviews you added to the view
use the following code
for (UIView *view in [self.view subviews])
[view removeFromSuperview];
add a comment |
to remove all the subviews you added to the view
use the following code
for (UIView *view in [self.view subviews])
[view removeFromSuperview];
to remove all the subviews you added to the view
use the following code
for (UIView *view in [self.view subviews])
[view removeFromSuperview];
edited Oct 19 '18 at 15:13
yoAlex5
3,39812421
3,39812421
answered Jun 9 '10 at 12:49
macmac
3,13072533
3,13072533
add a comment |
add a comment |
I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)
Anyway, this is the little helper method that I use for that:
- (void)removeAllSubviewsFromUIView:(UIView *)parentView
for (id child in [parentView subviews])
if ([child isMemberOfClass:[UIView class]])
[child removeFromSuperview];
EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?
Am using that now as follows:
// Make sure the background and foreground views are empty:
[self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
I like that better.
add a comment |
I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)
Anyway, this is the little helper method that I use for that:
- (void)removeAllSubviewsFromUIView:(UIView *)parentView
for (id child in [parentView subviews])
if ([child isMemberOfClass:[UIView class]])
[child removeFromSuperview];
EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?
Am using that now as follows:
// Make sure the background and foreground views are empty:
[self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
I like that better.
add a comment |
I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)
Anyway, this is the little helper method that I use for that:
- (void)removeAllSubviewsFromUIView:(UIView *)parentView
for (id child in [parentView subviews])
if ([child isMemberOfClass:[UIView class]])
[child removeFromSuperview];
EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?
Am using that now as follows:
// Make sure the background and foreground views are empty:
[self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
I like that better.
I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)
Anyway, this is the little helper method that I use for that:
- (void)removeAllSubviewsFromUIView:(UIView *)parentView
for (id child in [parentView subviews])
if ([child isMemberOfClass:[UIView class]])
[child removeFromSuperview];
EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?
Am using that now as follows:
// Make sure the background and foreground views are empty:
[self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
I like that better.
edited Nov 12 '18 at 4:36
Cœur
17.5k9104145
17.5k9104145
answered Feb 21 '14 at 6:07
Erik van der NeutErik van der Neut
1,84011618
1,84011618
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3005540%2fhow-to-remove-subviews-in-objective-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown