App crashes when view controller popped from navigation controller - iphone

I am pushing an object on navigation controller but when I return the control from the that view controller it crashes the app.
[self.navigationController pushViewController:chequeDetails animated:YES];
[chequeDetails release];
but when I write the same code with
[self.navigationController pushViewController:chequeDetails animated:YES];
chequeDetails=nil;
[chequeDetails release];
The app does not crash but a slight lag is observed... when i pop back from the check details controller?

If your seconds exaple code, you are not release the chequeDetails since calling release on a nil object does not nothing:
[self.navigationController pushViewController:chequeDetails animated:YES];
chequeDetails=nil;
// calling the release on nill will do nothing
[chequeDetails release];
Normally you can do it this ways:
[self.navigationController pushViewController:chequeDetails animated:YES];
[chequeDetails release], chequeDetails = nil;
But only release the chequeDetails if you did a alloc, init like:
ChequeDetails *chequeDetails = [[ChequeDetails alloc] initWithNibName:#"ChequeDetails" bundle:nil];
So the full code should be something like:
ChequeDetails *chequeDetails = [[ChequeDetails alloc] initWithNibName:#"ChequeDetails" bundle:nil];
[self.navigationController pushViewController:chequeDetails animated:YES];
[chequeDetails release], chequeDetails = nil;

I dont Know Exat... but i think you need to create a object of your delegate and you need to write appDelegate.navigationController rather then self.navigationController...
Note: appDelegate is a object of Delegate.

Related

presenting a view not working in iphone

I am trying to navigate from one page to another on a button click in a facebook appliaction.
I am using
registerUser=[[RegisterPage alloc]initWithNibName:#"RegisterPage" bundle:nil];
[self presentModalViewController :registerUser animated:YES];
for presenting the next view after getting response from facebook.
But it is not showing the next view. It works fine in all other places where I used to present other views.
Anyone have idea about this issue?
What exactly is 'self' here? Is it a viewcontroller? Or just a UIView?
I think this'll only work if self is a viewcontroller or some subclass of it.
My code to present a view controller is somewhat like yours (without a nib):
ViewController *controller = [[ViewController alloc] initWithNibName:nil bundle:nil];
[controller setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:controller animated:YES];
[controller release];
And to present a navigation controller it's like this (without a nib):
ViewController *controller = [[ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
[navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:navController animated:YES];
[navController release];
[controller release];
Sometimes simply copying someones code helps.
thanks for everyones reply.i have solved it after a long fight.i just dismissed the view before pesenting the next view and set dismissmodalviewcontrollerAnimated to NO.[self dismissModalViewControllerAnimated:NO];
nextview = [[LoginPage alloc]initWithNibName:#"LoginPage" bundle:nil];
[self presentModalViewController: nextview animated:YES];hope this help someone like me

switch between xibs on button action

I'm having difficulty in switching between xibs :(
first i was using navigationController and it worked well but i want to do it without using navigationController.
I tried presentModalViewController but it crashes my application. :((
Here is the code :
myViewController *viewController = [myViewController alloc] initWithNibName:nil bundle:nil];
myViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: myViewController animated:YES];
[myViewController release];
It is not working, ERROR received :
GDB:Program received signal : "EXC_BAD_ACCESS"
myViewController *viewController = [myViewController alloc] initWithNibName:nil bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: viewController animated:YES];
this should work. your problem was that you declared a view controller and named it "viewController", but you were not using that. you were using the view controllers name, not the declared instance.
You have set your nibName to nil
Enter the name of your Xib file you want to display there
Like
myViewController *viewController = [[myViewController alloc] initWithNibName:#"nameOfYourXIB" bundle:nil]autorelease];
myViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: myViewController animated:YES];
remove the release statement from ur code and everything will work fine.
Actually u r releasing the viewcontroller before it is shown as the superview.
//[myViewController release];
Hope this helps u...
Don't release he controller its a global variable i suppose so just make it in nil in dealloc

pushViewController and Release

I am trying to do like this, with the commented lines it works good, without, when I came back from the "pushed" view my App just crashes... when should I [release]? Or, better, I am doing this correctly?
if (indexPath.row == 1) {
Credits *cr = [[Credits alloc] initWithNibName:#"Credits" bundle:nil];
[self.navigationController pushViewController:cr animated:YES];
//[cr release];
}else{
Search *sr = [[Search alloc] initWithNibName:#"Search" bundle:nil];
[self.navigationController pushViewController:sr animated:YES];
//[sr release];
}
You should release your controller after pushing it onto the navigation view controller's stack of controllers. My guess is that something else is going on in the dealloc of your Search and Credits objects, that you are overreleasing an object there.

is this lead to memory leak for iphone

Let me explain in detais
In appDidfinish()
{
preLoginNavController = [[PreLoginNavController alloc] initPreLoginNavController];
[window addSubview:[preLoginNavController view]];
}
then in preLoginViewController when user press a button
then i am doing this to go to view2
RootViewController *arootController= [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:arootController animated:YES];
if i do this [arootController release]; then i cant come form view 2
now in view 2 when back button is pressed
then i am doing this
[self.navigationController popToRootViewControllerAnimated:YES];
so i cannot release [arootController release] else when i go to back view app quits with no error
and i need a prelogin view before Rootview thats why i did like that now what should i do .. my app is working fine but i want to fix that leak :(
HEY
i am getting this message when i click back button in view 2 after push and release in preLogin(1st view)
objc[408]: FREED(id): message release sent to freed object=0x466a340
I think yes, is leaking arootController once you pop it.
yeah, there will have a leak. 2 suggested solutions are:
[arootController autorelease];
or after you do :
[self.navigationController pushViewController:arootController animated:YES];
you can release it.
A good practice is that : who increase the retain Count, should decrease it. and because aRootController is init in that class, it should be released there
Edit:
This should be the correct code if you want to use navigationController:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.preLoginNavController = [[[PreLoginNavController alloc] init] autorelease];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainItemListViewController];
[window addSubview:[self.navigationController view]];
[window makeKeyAndVisible];
}
then when you need to push:
[self.navigationController pushViewController:anotherViewController animated:YES];

presentModalViewController not working properly

I have 3 views.
I want to do the following:
A presents B modally
A dismisses B
A presents C modally
I have setup a delegate pattern where A is B's delegate. This is how I am presenting and dismissing in B:
[delegate dismissB]; //this is just [self dismissModalViewControllerAnimated:NO]
[delegate presentC]; //this is just [self presentModalViewController:c animated:NO];
For some reason my app crashes when I execute this code with no debugger results (I have NSZombieEnabled).
When I comment out [delegate presentC] the app will dismiss B properly. When I comment out [delegate dismissB] the app does nothing, even though the line executes. I am not sure why?
UPDATE:
Here is the code in A
-(void)showARView{
[self dismissModalViewControllerAnimated:NO];
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];
arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
arTabBarController.delegate = self;
arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
arTabBarController.selectedViewController = arNavController;
[arNavController release];
[lbNavController release];
[self presentModalViewController:arTabBarController animated:NO];
}
Here is the code in B
[delegate showARView];
When you call dismissB the delegate dismisses the viewcontroller. Under normal circumstances (if you do not retain it elsewhere) this leads to the viewcontroller to be dealloced synchronously. And afterwards you are trying to access the delegate instancevariable, but for this the code needs a sane (hidden) self pointer, which is dealloced. I'm not sure if NSZombie can help in this case. You can easily find out if this is the reason for your crash, by inserting [[self retain] autorelease]; before [delegate dismissB];. However this is a hack and not a fix. You have a design problem.
This is not the way delegates are meant to be used. B presents some userinterface and receives some user interaction. It should then tell A what has happened via a delegate message, e.G. bWasCanceled or bFinished. It is the duty of the delegate, in your case A, to decide what to do next. So in your case the delegate may decide to dismiss B and instead present C. Or in code:
// Inside A
- (void)controllerB:(UIViewController*)ctl didFinishWithResult:(id)something {
[self dismissModalViewControllerAnimated:NO];
// Instantiate and initialize c
[self presentModalViewController:c animated:NO];
}
// Inside B
[delegate controllerB:self didFinishWithResult:#"OK"];
If I completely misinterpreted your code, and everything is fine there, I have an other suggestion. I have seen strange issues when dismissing and presenting modal viewcontrollers in the same round of the runloop. You may try [delegate performSelector:#selector(presentC) withObject:nil afterDelay:0.0]; and see if that helps.