I'm developing an iPhone and iPad application with Xcode 4.2 and latest SDK.
I have created a Tabbed Application without using ARC and I've found this on AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil] autorelease];
} else {
viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController_iPad" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController_iPad" bundle:nil] autorelease];
}
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
May I need to release viewController1, and viewController2?
Sending autorelease will cause the release message be sent later. So there is nothing to worry about, they will be released.
No. They will be sent autorelease, so they won't need to be released by you coding it in.
Sending autorelease just add them to the current NSAutoreleasePool which is drained at the end of each runLoop. So no need to additionally release them using release.
Tip: if it's a very large object (or many objects for example created in a loop) you really want to release immediately to dealloc it from memory, call release for immediate effect and reducing memory footprint.
Related
When I run this following code it gives above error
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
self.firstViewController=[[FirstViewController alloc]initWithNibName:nil bundle:nil];
UINavigationController *localNavigationController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.navigationController=localNavigationController;
[localNavigationController release];
UINavigationController *localFistNavigationController=[[UINavigationController alloc]initWithRootViewController:self.firstViewController];
self.firstNavigationController=localFistNavigationController;
[localNavigationController release];
NSArray *twoBars=[[NSArray alloc]initWithObjects:self.navigationController,self.firstNavigationController, nil];
UITabBarController *localTAbBarController =[[UITabBarController alloc]init];
[localTAbBarController setViewControllers:twoBars];
self.tabBarController=localTAbBarController;
[localTAbBarController release];
[self.window addSubview:self.tabBarController.view];
return YES;
}
if i run following code it runs well
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.firstNavigationController=[[UINavigationController alloc]initWithRootViewController:self.firstViewController];
NSArray *twoBars=[[NSArray alloc]initWithObjects:self.navigationController,self.firstNavigationController, nil];
self.tabBarController=[[UITabBarController alloc]init];
[self.tabBarController setViewControllers:twoBars];
[self.window addSubview:self.tabBarController.view];
return YES;
i not understood what is the difference between these. in first one i just created local variables & assigned those to properties. in later one directly used the properties.
why it is giving the error- program recieved signal "EXC_BAD_ACCESS"
I think in first one you releases some code and then after release you again that object like:
[localTAbBarController release]; this. So may be thats why you got error- program recieved
signal "EXC_BAD_ACCESS". in second one you nicely use your object no releases so its work
fine.
UPDATE:
hey i use your code, here you get BAD_ACCESS on this bellow line see..
[localNavigationController release];
just comment it and you have not BAD_ACCESS
i got my answer. because of releasing same objects more than one time, it happens.
i have released [localNavigationController release]; two times. later it must be
[localFirstNavigationController release];
Just check this line.
self.firstNavigationController=localFistNavigationController;
-->> [localNavigationController release];
It should be [localFistNavigationController release];
I'm new in iPhone, I created application that is started with UITabbarController with 4 items using AppDelegate. through the app I opened some views and I want to relaunch the AppDelegate again by using a code like:
[appdelegate presentModalViewController:myNavController animated:YES];
is this possible?
this is in my AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSMutableArray *array = [[NSMutableArray alloc] init];
MaktabatyTableViewController *own = [[MaktabatyTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *ownNavController = [[UINavigationController alloc] initWithRootViewController:own];
[array addObject:ownNavController];
NewestTableViewController *newest = [[NewestTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *newestNavController = [[UINavigationController alloc] initWithRootViewController:newest] ;
[array addObject:newestNavController];
MostBuyTableViewController *mostbuy = [[MostBuyTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *mostbuyNavController = [[UINavigationController alloc] initWithRootViewController:mostbuy];
[array addObject:mostbuyNavController];
FreeBooksTableViewController *free = [[FreeBooksTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *freeNavController = [[UINavigationController alloc] initWithRootViewController:free];
[array addObject:freeNavController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = array;
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window addSubview:self.tabBarController.view];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Thanks in advance.
I think you are looking for something like this..
A login screen which is a simple view with login fields, upon login, this screen is of no use. And the main app is based on tab bar.
And a Logout screen or A screen showing after user signed out.
I had this requirement in one of my app, so i made a sample template. May be it helps you checkout this.
This may be a stupid question but I programmatically added a UINavigationController to my app. If possible, I wanted to just add it to the top of all my windows except for the very first .xib. Maybe even just hide it on my first .xib. Is it possible to even do that? I think of my first .xib file that opens up to the rest of my app like a cover page and I rather that blue bar not show up at the top of that. I wish I could show you pictures but don't have enough reps yet. Thanks!
Below is the code I believe helps me to provide each page of app with the back bar:
#import "McCormick_TaylorViewController.h"
#implementation McCormick_TaylorAppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (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 = [[[McCormick_TaylorViewController alloc]
initWithNibName:#"McCormick_TaylorViewController" bundle:nil] autorelease];
UINavigationController * navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
in your McCormick_TaylorViewController's viewWillApper: method
just use bellow code...
[self.navigationController setNavigationBarHidden:NO animated:YES];
and in other view controller in navigationbar ot display then in another viewController's viewWillAppear just use bellow code..
[self.navigationController setNavigationBarHidden:NO animated:NO];
Use this method:
- (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 = [[[McCormick_TaylorViewController alloc]
initWithNibName:#"McCormick_TaylorViewController" bundle:nil] autorelease];
UINavigationController * navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
[navController.navigationBar setHiden:YES]; // hides navigation bar
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
I choose the project template as "Single View" .I am trying to push a detail view(UIViewController) on the button click.Button action is firing properly and executing the code [self navigatingController] push......My code is As:
MapDetailViewController *mapDetailViewController = [[MapDetailViewController alloc] init];
[self.navigationController pushViewController:mapDetailViewController animated:YES];
NSLog(#"self.navigation is as %#",self.navigationController);
self.navigationController is null.So i tried to alloc init a local variable of type Navigation Controller and also the variable was able to be non-null(some hex value), still i was unable to push the mapDetailView.
what i am thinking is that i have choosen the wrong template(view based).Should i choose the Navigation-based(Master-detail).Well the below auto generated code is of application delegate:
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;
Should some changes be made here for navigation controller?
Any Suggestion?
Your problem is that you don't even have a navigation controller to do the pushing. Change your code to 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 *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I have ViewController(Passwordviewcontroller) which I want to show with "presentModalviewController"
I have an AppDeleage:
- (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;
}
Here is My ViewController from which I want the PasswordviewController to show:
-ViewDidLoad
{
self.passwordView = [[PasswordView alloc]initWithNibName:#"PasswordView" bundle:nil];
[passwordView setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:passwordView animated:YES];
}
I tried everything but its still not working, has somebody an Idea?
Put this in viewDidAppear instead of viewDidLoad. In viewDidLoad your view might be loaded into memory but doesn't have to be on-screen yet. In viewDidAppear on the other hand your view is ready to be shown an properly inserted in the window in such a way that you may show modal views.
Edit: Changed viewWillAppear to viewDidAppear as per comments