removeFromSuperview removes all subviews - iphone

- (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

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];
}
}

Change a UIActionSheet's UIAlertButton's font

How can I gain access to a UIAlertView button's titleLabel property? I've tried the following code:
for (UIView *view in action.subviews)
{
if([view isKindOfClass:[NSClassFromString(#"UIAlertButton") class]])
{
//Do something with title label font
}
}
try like this ,you can change the view.tag based on your requirement
- (void)willPresentAlertView:(UIAlertView *)alertView{
for(UIView *view in alertView.subviews)
if(([view isKindOfClass:[UIButton class]]) && (view.tag ==1)){
UIButton *btn = (UIButton *)view;
btn.titleLabel.text=#"your title";
}
}

How can i remove all UIViews from rootViewController

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

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];
}
}

Looping through specific class in view : Objective-c

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:.