Memory leak while assigning rootviewcontroller - iphone

There is a 100% of memory leak shows by instrument at self.window.rootViewController= navigationController;. Application uses ARC.
UINavigationController,UIViewController and window are all properties have attribute strong.
How can i fix this leak.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
txnObserver = [[InAppPurchaseObserver alloc] init];
txnObserver.delegate = self.viewController;
[[SKPaymentQueue defaultQueue] addTransactionObserver:txnObserver];
navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
**self.window.rootViewController= navigationController;**
[self.window makeKeyAndVisible];
return YES;
}
Same code did not show any leak in iOS 6 but it shows leak in iOS 7.
Updated question on 2013/10/10 with details.

You can change your didFinishLaunchingWithOptions by this following code without #property of viewController :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
with #property (nonatomic, strong) ViewController *viewController; :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}

Related

can't navigate to another ViewController

I'm newbie. There is a button in ViewController.m . When I press the button it has to navigate to SecondViewController but SecondViewController doesn't appear.
And at the SecondViewController on the navigation bar, there will be "Back" button to go back to ViewController. Can you tell me what I'm missing?
ViewController.m:
-(IBAction)buttonPressed:(id)sender
{
EnglishViewController *v = [[[EnglishViewController alloc]
initWithNibName:#"EnglishViewController"
bundle:nil]
autorelease];
[self.navigationController pushViewController:v animated:TRUE];
}
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
The solution (depends on #George's answer):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *navCon =[[[UINavigationController alloc]
initWithRootViewController:self.viewController]
autorelease];
self.window.rootViewController =navCon;
[self.window makeKeyAndVisible];
return YES;
}
When you create your ViewController in AppDelegate.m you must surrond it with navigationController to make it work
Something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[navigation release];
[self.window makeKeyAndVisible];
return YES;
}
The first few things to check: is your -buttonPressed method actually getting called? Is are you getting and passing a non-nil value to -pushViewController:animated:? Is self.navigationController actually non-nil when you make that call?
Your code looks fine as written.

Application running with RootviewController error- Applications are expected to have a root view controller at the end of application launch

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I do not know what is wrong with this method.I just created a new project and run it.It showing Applications are expected to have a root view controller at the end of application launch
If you have a MainWindow.xib.
remove below line. do not remove MainWindow.xib
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
OR
do not remove above line. remove MainWindow.xib and Project Summary Main Interface set null.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

uinavigation bar in all view

In my project the navigation bar is being coming only in rootview(homeview) for the first only,i want to enable navigation bar in all views?Here my code?What change should i do for that
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.window.rootViewController =navigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Please help me to code?
See this link
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
navigationController =[[UINavigationController alloc] init];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[navigationController pushViewController:viewController2 animated:NO];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And when you navigate to the another view from viewController2 do pushViewController not others like presentView
[self.navigationController pushViewController:anotherViewController animated:YES];
Put this code in a delegate class.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
If you want to enable navigation in all the views then you will have to declare the navigation in AppDelegate.m wherein it will be viewable in all the views. I dont have my mac rite now but its the best advice I can provide for now :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.window.rootViewController =navigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Change the uiviewcontroller to SecondViewController
try this way may be helped you
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// [NSThread sleepForTimeInterval:0.1]; // simulate waiting for server response
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
// Initialise the navigation controller with the first view controller as its root view controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
// This is where we hide the navigation bar! :)
[navigationController setNavigationBarHidden:NO];
// Navigation controller has copy of view controller, so release our copy
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[self.viewController release];
// Add the navigation controller as a subview of our window
[_window addSubview:[navigationController view]];
[_window makeKeyAndVisible];
return YES;
}

How to set UIVIewController to RootViewController?

I want to know that how can we setup a UIViewController in a UINavigationController without using UITableView to its Root View Controller. Any help regarding this? Thanks in advance.
You can do that as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
UIViewController *myViewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
self.window.rootViewController = navigationController;
// ...
}
You can replace the class of myViewController and/or initialize it using a nib if you want.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:[[OptionViewController alloc]initWithNibName:#"OptionViewController" bundle:nil]];
self.window.rootViewController=nav;
self.window.backgroundColor = [UIColor grayColor];
[self.window makeKeyAndVisible];
return YES;
}
It's working for me:
set your tabviewcontroller on window, then it will work properly.
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UITabBarController *tabVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MyMainView"];
[window setRootViewController:tabVC];
UINavigationController pushViewController:animated:

set class for UINavigationController

How to set class(first class) for UINavigationController from code ?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
LoginViewController* loginViewctrl = [[LoginViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewctrl];
UIWindow* window addSubview:[navigationController view]];
[loginViewctrl release];
[window makeKeyAndVisible];
}