How to remove subview from Superview? - iphone

I have taken one subView of type UIView on the top of UIViewController. I want to remove it and again load it after clicking on button. But I am unable to remove it.
I have used [subView removeFromSuperview] method. But it is not working.

Try out the method inside the AppDelegate. If you have loaded the rootViewController.view as a subview to your window, try to unload it again, you should see a white screen.
[self.window addSubview:rootViewController.view];
[rootViewController.view removeFromSuperview];
Also, insert this line before you removeFromSuperview in your code:
NSLog(#"%#",[rootViewController.view superview]);
Replace rootViewController.view with your view. If the log reads (null), it means you have incorrectly assigned your view as a subview.

Related

UIview doesn't load with viewdidappear

I have a tableview project which has a view, everything that I do in this view is controlled by "viewdidappear" method.
I added a new UIview which has "lines.h" and "lines.m" files.
I use this view to draw some lines and works god.
This extra view is located in to a scroll view because is to long to display in to iPhones screen.
The problem is that viewdidappear doesn't reload the uiview data, it only works with viewdidload.
I have to close the application and reopen it to get my data reload.
First, try adding this line at the end of viewDidAppear method:
[self.view setNeedsDisplay];
Or:
[_linesView setNeedsDisplay];
Second, Provide some more information. Post the code in viewDidLoad and viewDidAppear.
Try calling [self setNeedsDisplay] in the lines UIView class to cause the view to redraw itself.

UIview doesn't work property with viewdidappear [duplicate]

I have a tableview project which has a view, everything that I do in this view is controlled by "viewdidappear" method.
I added a new UIview which has "lines.h" and "lines.m" files.
I use this view to draw some lines and works god.
This extra view is located in to a scroll view because is to long to display in to iPhones screen.
The problem is that viewdidappear doesn't reload the uiview data, it only works with viewdidload.
I have to close the application and reopen it to get my data reload.
First, try adding this line at the end of viewDidAppear method:
[self.view setNeedsDisplay];
Or:
[_linesView setNeedsDisplay];
Second, Provide some more information. Post the code in viewDidLoad and viewDidAppear.
Try calling [self setNeedsDisplay] in the lines UIView class to cause the view to redraw itself.

How can I add a view from subviews on top of its superview?

I have a mainViewController and inside its nib file I added an info button, in which action is to flip between two subviews, subview A and subview B.
From mainViewController, under viewDidLoad, I'm inserting subview A. Here I notice that the info button is in front of the subview A, which is fine.
The problem comes that when pressing any buttons that are located within subview A's nib file, in which they add new subviews, the info button remains on front.
So, how can I add these later subviews on front of all parent view stacks, so the info button does not appear? or how can I hide the info button?
You can put subview infront of everything else by calling
[view bringSubviewToFront:subview];
You can hide the button (or any other UI element) by calling it's setHidden function like so:
[button setHidden:YES];
always application can only contain a one window at a time. so this is the best way.
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:yourView];

troubles adding splash screen subviews to UIwindow

I have an app that has a login screen and splash screen. I want these screens to show up one after the other, and as such I have resulted to adding them as subviews to the app delegate's window property.
[window addSubview:mainViewController.view];
[window addSubview:loginViewController.view];
[window addSubview:splashScreenViewController.view];
The problem is that each subsequent subview that is added seems to appear in a weird manner(the orientation of the nib file is not obeyed). I know my nib files are not at fault, because if I only include one view, no matter which view it is, it shows up right. Furthermore, if I add the views as subviews to the main view controller's view(as opposed to app delegates) view, i dont get this issue. However, there is a 20px gap at the bottom of the screen because I guess UIWindow accounts for the status bar, and normal UIView's do not.
Does anyone have any idea what the problem might be? Is UIWindow doing something special? Am I not supposed to add several subviews to UIWindow? If anyone has tips regarding how to do splash screens, that would be much appreciated.
I suggest you use a UIView in the middle...
UIView view = [[UIView alloc] init];
[view addSubview:mainViewController.view];
[view addSubview:loginViewController.view];
[view addSubview:splashScreenViewController.view];
[window addSubview:view];
[view release];
After what if you what to animate as a splash screen, take a look at one of my post :
how to open a new uiview like a popup?
You should only have 1 view controller at a time.
You should use the -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController delegate function to check which tab was clicked. If it's your second view (which it should be if there are only 2 tabs) then call a custom method in your splashscreencontroller that pops up the modal. If that splash screen is only supposed to show once, make sure to set a flag afterwards to make sure it doesn't pop up again.
A TabBarController loads all the views before displaying them, so there's no lag when a user switches between tags.

Trying to remove Super ViewController but stuck in View?

I have 3 ViewControllers 1.Main 2.Gallery 3.Text. What my problem is that i have the GalleryViewController using an External UIView with the name GalleryItemView. So once the view controller calls this inside the UIView i have a button that changes the subview so what i do is use the [super addSubView:newView]; eveything works out great but when i return to the GalleryViewController my app crashes so im guessing im doing this wrong. also what i notice is that once i change Views from pressing the button inside the GalleryViewController takes me to the menu and when i do a swipe gesture it scrolls horizontal and i do have a scrollview in the GalleryViewController Class so maybe what im doing is adding a subview to the ScrollView anyone know how i can fix this?
For starters:
[super addSubView:newView];
is pretty much incorrect.
Adding a view to a viewcontroller, you want to do
[self.view addSubview:galleryItemView];
[self.view bringSubviewToFront:galleryItemView];
If you believe you have added it properly, you could do the following to verify it:
if ([galleryItemView.superview isKindOfClass:[UIScrollView class]]){
NSLog(#"galleryItemView's parent is a scrollview");
}
Please post the crash if you still have problems.