UINavigationController releasing bonanza - ios

At the end of this code:
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewController release];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
The navigation controller appear modally, as usual, but when I click a button on the viewController, it crashes. In fact, the viewController has a retain count of 0.
If I comment one of the two releases everything went better than expected.
I have been seeing this code pretty much everywhere, what could be wrong?

The code you've posted is correct, but somewhere else you're over releasing something.
A few things to note:
First, never trust retainCount.
Second, make sure you're properly managing the memory of your nib objects as outlined here.
Finally, you'll need to use the NSZombie detection in Instruments to find out where your real problem lies. This video provides a nice how to.

Related

Presenting view controllers on detached view controllers is discouraged - definition of "detached view controller"?

OK. So I've looked around the web and read similar questions about this relatively new iOS warning. My app uses NO storyboard. I only have one simple question. What is a "detached view controller" and can anyone provide a definitive reference (e.g. to an Apple doc) that provides a definition of "detached view controller" as we are to understand it in the context of this warning. I fail to see how other answers to this question are more than fumbles and guesses around the topic if no-one truly understands precisely what a detached view controller is.
A detached view controller is one that is not currently in the hierarchy of [[[UIApplication sharedApplication] keyWindow] rootViewController]
For example, I could instantiate a UIViewController anywhere in my code, but if I never put it in this hierarchy by making it my rootViewController or pushing onto a navigation stack in my rootViewController, then I would get strange/undefined behavior when I present something on it.
Example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController* a = [[UIViewController alloc] init];
UIViewController* b = [[UIViewController alloc] init];
UIViewController* someOtherVC = [[UITableViewController alloc] init];
self.window.rootViewController = a;
//Works fine :)
[a presentViewController:someOtherVC animated:YES completion:nil];
//might break the world
[b presentViewController:someOtherVC animated:YES completion:nil];
}

Segueing a view without Storyboard

I'm trying to figure out how to preform transitions between different views just with code, without storyboard. For example, on a button press.
- (IBAction)nextClass
{
// insert transition between views
}
I know this is possible, I was just wondering what the actual code would be to make this happen.
Not 100% sure if I am understanding your question correctly, but I think you can just say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
The name "ViewController" after initWithNibName should be the name of the .xib file that contains the UI for ViewController. Let me know if this is what you are looking for!
EDIT:
Mungbeans brings up a good point. If you are using a navigation controller, you should say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

UINavigationcontroller pushViewController not working to get next Viewcontroller

I red nearly all the help which is provided, and i'm still confused. I have simple project in Xcode4.2 with sotoryboard. There i have Navigationcontroller and two Viewcontroler (one, two). If i'm trying to get from one to two with pushViewController its break down. Where is my problem. If i have the Navigationcontroler in my storyboard i shouldn't have problem to access it and use it, is it right? I don't won modal window application i use something like story board where the user can move back and front without lose the controls data. How should i do it right.
thanks for any help I'm new to apple developing :)
The only code :
- (IBAction)Next:(id)sender{
two* myNext = [[two alloc] initWithNibName:#"two" bundle:nil];
NSLog(#"%#", [self navigationController]);
NSLog(#"%d", [[[self navigationController] viewControllers] count]);
[[self navigationController] pushViewController:myNext animated:YES];
// [[self navigationController] pushViewController:myNext animated:YES];
// [myNext release];
}
The solution is to change the initialize UIViewController because of using storyboard you can't use initWithNibName. You have to use instantiateViewControllerWithIdentifier which use identifier property of UIViewController. The identifier property can by set in the sotryboard.
ViewController* myNext = [self.storyboard instantiateViewControllerWithIdentifier:#"Next"];

Difference in ViewController pushing

I'm doing the CS193P Stanford course tutorials and also some Apple iOS dev tutorials, and there's a difference between how they push the ViewController to the screen
Apple does this:
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
Stanford suggests doing this:
navigationController = [[UINavigationController alloc] init];
[self.navigationController pushViewController:rootViewController animated:NO];
How are they different?
PS: btw, Apple's method work and the Stanford one doesn't display anything and I don't know why.
I think that using the pushViewController:animated method you are going to add the controller at the top of the stack of the controllers (push a new view controller on the stack).
In the second method you are not initializing the navigationController.

Dismissing iPad UIPopoverController from within its content controller

So I have a popover with a button in it. When that button is pushed, I want the popover to go away. It seems easy enough, but I can't figure it out for the life of me. The code I use to show the popover is below:
AddCategoryViewController* content = [[AddCategoryViewController alloc] init];
UIPopoverController* aPopover = [[UIPopoverController alloc]
initWithContentViewController:content];
aPopover.delegate = self;
[content release];
// Store the popover in a custom property for later use.
self.addCategoryPopover = aPopover;
[aPopover release];
[addCategoryPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Within the addcategoryviewcontroller, I have:
-(IBAction)saveAddCategory:(id)sender {
if (rootViewController == nil)
rootViewController = [[RootViewController alloc] init];
[rootViewController.addCategoryPopover dismissPopoverAnimated:YES];
[rootViewController dismissPopover];
}
Rootviewcontroller is where the popover is being created from. Unfortunately, neither of those methods work to dismiss it. any help?
You would be seeing a warning at this line.
aPopover.delegate = self;
and if you would execute your code. The app would crash. Instead you need to do it like this.
I have
- (void)viewWillDisappear:(BOOL)anAnimated
{
[self.dPopover dismissPopoverAnimated: NO];
self.dPopover = nil;
[super viewWillDisappear: anAnimated];
}
and don't see why this wouldn't work in your case.
Your if is a bit troubling, so my guess is you aren't talking to the view you think you are. rootViewController.addCategoryPopover is probably nil, because you made a new controller.
I think I answered just a similar question with the solution I used to dismiss a popover with a UIView loaded from a MKMapView.
The use of my solution is basically the same as for any other view loading a popover.
Have a look at:
How to dismissPopoverAnimated on iPad with UIPopoverController in MKMapView (SDK3.2). I hope that solved your problem.
use NSNotificationCenter To DissmissPoperController Fro Father viewControll

Resources