I am practicing multiview but having a small problem.
When my new view gets loaded, I can still see previous view.
How to make previos view invisible??
Here is my code :-
- (IBAction)changeView:(id)sender
{
RedClass *red = [[RedClass alloc] initWithNibName:#"RedClass" bundle:nil];
self.redClass = red;
[red release];
//[self.view removeFromSuperview];
[self.view addSubview:redClass.view];
}
and here is a screenshot :-
And when I uncomment [self.view removeFromSuperview] then my red view is not even getting loaded.
Any help would be appreciated.
Thanks
I think color of your red view may be transparant .
so please check it in your nib file of redclass. also check alpha for your red view it must be 1.
You can not remove your first view here. because it may be loaded at window or other controller. so if you are adding view2 in view 1 and removing view1 then view2 automatically get removed ,because it is already in view1 . so you can not write [self.view removeFromSuperview];
You can declare view variable in .h file. and alloc it in viewDidLoad and don't release it there. Whenever you want to add view use, [self.view addSubview:red]; and when you want to remove you can use [red removeFromSuperview];
[self.view removeFromSuperview] removes the uiview which you is displayed.
So you need to call removefromsuperview for previously loaded view and call addsubview for new one.
Related
I have a viewController and I am trying to add a subview to it such that it will cover the whole screen, however this has a navigationController in it so that adding a subView always adds it below the navigation bar, is there a way to simulate a presentModalViewController in cases like this?
You can add the subview to the view controller and then hide the navigation controller from the top or you could still push it to the navigation controller and then just remove the navigation controller from the top again and then you could use pop to go back and forth.
the code to push a view controller is
if(!self.YOURVIEWCONTROLLER){
self.YOURVIEWCONTROLLER = [[YOURVIEWCONTROLLER alloc] initWithNibName:#"YOURVIEWCONTROLLER" bundle:nil] autorelease];
}
[self.navigationController pushViewController:self.YOURVIEWCONTROLLER animatedLYES];
and on the next NEXTView.m add
[self.navigationController setNavigationBarHidden:YES];
remember to create an instance of YOURVIEWCONTROLLER in the .h file. Or you could do a simple
[self.view addSubview:NEWVIEW];
[self.navigationController setNavigationBarHidden:YES];
at least at bear minimum the line for making the navigationController hide is there.
I seem to recall once having a similar problem, and I seem to recall the solution was to add the subview to the navigation controller (as the view controller is already a sub view of the navigation controller) rather than adding it to the view controller.
I recently have similar problems and after spending 5 to 10 min I get the exact solution...
According to my solution I simply add my custom UIView to subview of navigationController.view
Like This :-
[self.navigationController.view addSubview:popOver];
popOver - Your custom UIView
Happy Codding :)
Add the view to the superview of the navigationController's view.
[navigationController.view.superview addSubview:viewController.view];
Perhaps you could hide the navigation bar when you add the subview. I have a method on my ViewController that looks like this:
self.navigationController.navigationBarHidden = YES;
UIView *v = [[UIView alloc] initWithFrame:self.view.frame];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
When that code executes, my navigation bar disappears and a full screen red view replaces it.
I have a ViewController which creates a NavigationController and puts a UIView in it.
nvcontrol =[[UINavigationController alloc] initWithRootViewController:menuView];
...
UIView *parent = self.view.superview;
[parent addSubview:nvcontrol.view];
I need to close the view (incl. the NavController) from within the child-viewcontroller.
The following snippet I tried, only closes the view inside the NavController, not the NavController itself. This ends up showing the blue head with a white space below.
[self.view removeFromSuperview];
What could solve this?
I have not tried this but I think something like this should work:
[self.navigationController.view removeFromSuperview];
#traingle_man Are you sure view is a property of uinavigation controller?
If I understand your question correct, this may help
[nvcontrol.view removeFromSuperview];
In my app I have a tabBarController and in it a navigationController. One of my view controllers is a TableViewController and under the navigationBar i added a uiView as a subview to the view like this:
rectangleInfo = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,26)]; rectangleInfo.autoresizingMask = (UIViewAutoresizingFlexibleWidth); rectangleInfo.backgroundColor = [UIColor darkGrayColor]; [self.view addSubview: rectangleInfo];
when I click on a cell in the tableView I push an UIViewController like this:
[feedViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[[self navigationController] presentModalViewController:feedViewController animated:YES];
After i pop the modal view for a couple of times with it from the tableViewNavigationController disappears the rectangleInfo UIView.
I pop my modalview like this:
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self dismissModalViewControllerAnimated:YES];
any idea why that subview (rectangleInfo) of the tableViewController dissapears after i remove the modal view from the superview?
thank you in advance.
I'm curious as to what you are trying to do with your rectangleInfo view? By the size of it, it looks like you are trying to mimic the status bar. Why? You can just hide the status bar if you want.
Another thing you can try is to create the UIView visually in Interface Builder. Don't actually add it to your main view, but create it as a separate UIView in the XIB with the appropriate size, etc. Then create an outlet for it in Xcode and connect it. Next, add it as a subview in code when your view controller loads. See if that makes a difference. This is especially strange since you say it only disappears after popping it several times. Do you get the same problem if you push and pop the non-modal way, e.g.:
[[self navigationController] pushViewController:feedViewController animated:YES];
[[self navigationController] popViewControllerAnimated:YES];
i solved the problem by implementing correctly the viewDidLoad and viewDidUnload methods for creating and releasing my subviews.
In a view based app, I display a view and once the user is finished with that view, Done is clicked and the view is removed. However, that does not dealloc the view, which is what I want to do. What is the proper way to dealloc this type of view?
Currently, I'm nil'ing out the second view before it is called. That seems to work and the second view is reinitialized. However, isn't it more appropriate for the second view to destroy itself (nil itself after removeFromSuperview)?
In first view:
//show next view
aView = nil;
if(aView == nil)
{
aView = [[AView alloc] initWithNibName:#"aView" bundle:nil];
}
[self.view addSubview: aView.view];
Click Done in aView
[self.view removeFromSuperview];
The method removeFromSuperview will automatically release "aView.view", so you shouldn't release aView in the first view controller. I think you've declared AView *aView in head file, but you don't need to. You may declare the aView as a local variable like this:
// go to second view
SecViewController *sec = [[SecViewController alloc] initWithNibName:#"SecView" bundle:nil];
[self.view addSubview:sec.view];
// go back
[self.view removeFromSuperview];
Immediately after
[self.view addSubview: aView.view];
You should add:
[aView release];
Your subview has been retained by your view controller's view so can be release.
If you previously set your viewController to first responder status (like to respond to motion events) the responder chain will retain the controller. You must resign the first responder to destroy the controller completely.
[self resignFirstResponder];
[self.view removeFromSuperview];
both of the above answers are correct theory for how you should memory manage in objective c.
as per the dev documentation:
http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/removeFromSuperview
callling removeFromSuperview will actually call release FOR you, so you are okay, I think.
I have seen the post for How to switch views by buttons on iPhone? but this doesn't answer how to switch back and forth between views with buttons. The person that asked the question settled on the answer that they could switch between views with uinavigationcontroller.
I put the following code in an ibaction that kicks off when a button is pressed in the primary view.
PhoneNumberViewController *phoneNumberViewController1 = [[PhoneNumberViewController alloc] initWithNibName:#"PhoneNumberView2" bundle:nil];
self.phoneNumberViewController = phoneNumberViewController1;
[self.view removeFromSuperview];
[self.view insertSubview: phoneNumberViewController1.view atIndex:0];
When this code executes the whole view just goes blank. If I omit the removefromsuperview portion then the view disappears behind my button but the button still remains. I'm not sure if this is the right way to switch between buttons but if anyone knows how to do this please help. Also if anyone knows about any example projects that switch between views with buttons let me know.
Thanks a million!
You removed the view controller's view from it's superview and then added a subview to it. The view hierarchy is broken at the view controller's superview (likely your window). That is why you are getting a blank screen.
You'd likely want to keep a reference around to the original view and then swap it out to the new view by setting the view controller's view to the new view.
// origView is an instance variable/IBOutlet to your original view.
- (IBAction)switchToPhoneView:(id)sender {
if (origView == nil)
origView = self.view;
self.view = phoneViewController.view;
}
- (IBAction)switchToOriginalView:(id)sender {
self.view = origView;
}
The technique I usually use involves creating a superview class which contains a toolbar at the bottom, and a content view UIView class filling the rest of the screen.
I then add subviews to the content view to change the views based on button clicks. This approach makes it so the toolbar on the same is constant across all views. I start by defining a helper function like this:
-(void) clearContentView {
//helper to clear content view
for (UIView *view in [self.contentView subviews]){
[view removeFromSuperview];
}
}
My IBAction then looks like this:
-(IBAction) buttonClicked{
self.title = #"Images"; //change title of view
[self clearContentView]; //clear content view
[self.contentView addSubview:self.imagesViewController.view]; //add new view
[self.imagesViewController viewWillAppear:YES]; //make sure new view is updated
[self enableButtons]; //enable all other buttons on toolbar
self.imagesButton.enabled = NO; //disable currently selected button
}