I have built a program without a menu. The main view has custom buttons which loads XIBs depending on which button is pressed. The only issue i'm having is when I go back to an already used view, its reset. I have tried "retain" in viewDidUnload and in viewWillUnload. I have tried everything I can thin of and cant get it to work.
- (IBAction)gotoMusicView:(id)sender{
//[self.view addSubview:musicview];
if(self.musicMenuData == nil)
{
musicMenu *musicview = [[musicMenu alloc]initWithNibName:#"musicMenu" bundle:[NSBundle mainBundle]];
self.musicMenuData = musicview;
musicview.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:musicview animated:YES];
[musicview release];
}
musicMenu *musicview = [[musicMenu alloc] initWithNibName:nil bundle:nil];
musicview.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:musicview animated:YES];
}
That musicview loads fine. After I leave that view and go back to the main view, it is RELEASED. Therefore when I reopen "musicview" everything , such as my UIWebView, has reset as if I am opening it again for the 1st time
Create a singleton class using this
And then initialize your .xibs there so you can use the same instance everytime you load the .xib. This way it'll never reset
Instead of allocating and initializing each time you need to use it, keep a reference of all the view controllers you're using, check of its nil on the first time if so initialize, example below.
if(self.someViewController == nil)
self.someViewController = [[ViewController alloc] initWithNibName:#"ViewController"....
//Now present self.someViewController..
Do this for the view controllers that you use for the tabs.
Related
I have some view controller which I call with the following method:
myViewController *myView = [[myViewController alloc] initWithNibName:nil bundle:nil];
myView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:myView animated:YES];
[myView release];
if I use the app a few times I get a memory warning and the app freezes for a few seconds! I think the reason is that i switch the view but not discharged the old one !!?!!?!!
(i set my outlets to nil and release them)
how can I unload the old view after switching to the new one?
Thanks in advance
When switching the view be sure to call dismissModalViewController:(BOOL)animated on myViewController.
In the class that launch the modalViewController you could make a property for the modal viewcontroller which you retain. Then you could write something like this.
//This would be in an action or something...
if (self.myViewControllerProperty == nil) {
self.myViewControllerProperty = [[[MyViewController alloc] initWithNibName:nil bundle:nil] autorelease];
}
[self presentModalViewController:self.myViewControllerProperty animated:YES];
Then instead of setting the
myView.modalTransitionStyle =
UIModalTransitionStyleCoverVertical;
Move that code to the modalViewController and write self.modalTransitionStyle = UIModalTransitionStyleCoverVertical; I think that looks cleaner, keep the configuration of each viewcontroller separted don't mix it up.
And as the maclema said, call dissmissModalViewController, but you probably are doing that...
Could be any number of problems but you don't need to (and can't) unload the old view. Make sure you are releasing objects and setting outlets to nil in viewDidUnload of all of your view controllers. viewDidUnload will be called when a memory warning occurs so if you don't handle it correctly you'll have leaks and can crash. Other than that, hard to know what else your app is doing that is contributing to the crash.
I have some pretty simple code where I am using a UINavigationController and adding a rootViewController. After some processing has occurred I want to pop off the current view controller and replace it with another one. This seems to work fine, but my original view controller does not dealloc. I set a breakpoint in it's dealloc and it never gets hit. Below is my code. Not sure why happens. Just for testing if I release startController twice it does go away.
StartViewController *startController = [[StartViewController alloc] initWithNibName:#"StartViewController" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:startController];
[nav pushViewController:startController animated:NO];
self.navController = nav;
[startController release];
[nav release];
Thanks any help.
Your ultimate goal is to bring the view controller's retain count to zero. So make sure that everything the view retains is released and anywhere the view is retained also release.
Please check the following possible causes:
Make sure you pop the view controller from the navController if you have a custom back button. The default Back button will work fine.
Make sure that all your IBOutlets are set to nil in viewDidUnload
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
}
If your view is an observer to a model class to receive events
For example
model.addObserver(myView);
and sure to also do
model.removeObserver(myView);
I hope this helps.
It looks as though your self.navController has got a holding reference to it. maybe put
self.navController =nil;
somewhere appropriate, so that when the view has been popped it is released.
I was trying to pop off the root view controller. I instead used the setViewControllers message from the UINavigationController object to replace all my view controllers.
I'm confused something fierce over having multiple views. I simply want to have a button on my main view that activates a new view, which in turn would have an (x) button which goes back to main view. For the life of me, I can't figure out how to do this with two separate .xib files. How might this be done?
Thanks!
It might be easiest to just use the utility application as a template.
From there, you can see how you would load view controllers and nibs in order to bring up the new view followed by how you would exit it.
I actually solved it by doing it this way:
NewViewController *new = [[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil];
new.delegate = self;
new.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:new animated:YES];
[new release];
You are using a navigation controller, right?
On the IBAction associated with a button tap on the main view, put
NewViewController *newView = ... // [alloc - init your view here if you haven't already]
[self.navigationController pushViewController:newView animated:YES];
And on the button on newView,
[self.navigationController popViewControllerAnimated:YES]
In order to speed up my app, I've create three different UIViewController in AppDelegate and it has readonly property for the controllers. Those controllers are used for navigation controller.
If I tap a button on the root view, I just show another view using pushViewController method. Let me show you some code for this here.
UIViewController* controller = delegate.anotherViewController;
[delegate.navigationController pushViewController:controller animated:YES];
At first time, this work well, but if I navigate back and tap the button again, I've got a signal 'EXC_BAD_ACCESS' at second line.
What's wrong? And, how can I prepare all of my view controllers at the beginning, not create them when they are needed?
Most of the time EXC_BAD_ACCESS means that you've released an object and you're trying to reuse it without retaining it.
Look if you have released your viewController too early and whether you are (re)using it the right way or not...
I had the same problem. My code was
AddMedia *info = [[AddMedia alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:info animated:YES];
[info release];
I was releasing my viewCOntroller which was crashing the app.
When I commented that line, It worked seamlessly. The code after the change is:
AddMedia *info = [[AddMedia alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:info animated:YES];
// [info release];
I have 3 different xib's. I am able to go back and forth between view 1 and view 2 with the following code...
This Code brings up the second view...
-(IBAction)startButtonClicked:(id)sender{
self.gamePlayViewController = [[GamePlayViewController alloc] initWithNibName:#"GamePlayViewController" bundle:nil];
[self.navigationController pushViewController:gamePlayViewController animated:YES];
[GamePlayViewController release];
}
This Code is executed in the second view and brings me back to the first view...
-(IBAction)backButtonClicked{
[self.navigationController popViewControllerAnimated:YES];
}
Now when I try to execute this code (in the second view) to get to the third view...I get SIGABRT and the app crashes...why does it work for the first view bringing up the second view, but not for the second view bringing up the 3rd view?
-(IBAction)nextView{
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
[self.navigationController pushViewController:thirdViewController animated:YES];
[thirdViewController release];
}
There's probably some object or outlet in ThirdViewController.xib that you've forgotten to configure or misconfigured. Compare and contrast ThirdViewController.xib and GamePlayViewController.xib, paying close attention to how you've configured ThirdViewController.xib's class names and outlets. In particular:
Make sure your File's Owner is properly set to ThirdViewController.
Make sure the view outlet of the File's Owner is properly connected to its view.