program received signal "EXC_BAD_ACCESS" - iphone

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];

Related

relaunch the iphone app by AppDelegate programmatically

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.

iOS tab bar not show modal view

I want to show modal view in the start of the app but it doesn't appear. This is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self initDataBase];
CustomerModel *customer = [[[CustomerPersistor alloc] init] getLoggedCustomer];
self.window.rootViewController = self.tabBarController;
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *profileViewController = [[[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil] autorelease];
profileViewController.title = #"Profile";
UINavigationController *profileNavigation = [[[UINavigationController alloc] initWithRootViewController:profileViewController] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:profileNavigation, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
if(customer == nil) { NSLog(#"HO");
ViewController *login = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//login.delegate = self.tabBarController.view;
[self.viewController presentModalViewController:[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] animated:YES];
}
return YES;
}
How can I fix it?
First, there doesn't appear to be a self.viewController property that is set, and you're instantiating the same "ViewController" twice. This is all wrong:
ViewController *login = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//login.delegate = self.tabBarController.view;
[self.viewController presentModalViewController:[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] animated:YES];
Try moving this code to your viewDidLoad of your profileViewController...i.e. let the TabBarController load along with its first tab's view controller.
Initiate an instanse from the model view like this:
modelView = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
Please follow the list below:
Set the viewController model presented style
[modelView setModalPresentationStyle:UIModalPresentationPageSheet];
Then present it using:
[self.viewController presentModalViewController:floorView animated:YES];
Then, set the size and location of the model controller like this:
modelView.view.superview.bounds = CGRectMake(0, 0, 700, 550);//it's important to do this after presentModalViewController
modelView.view.superview.center = self.view.superview.center;//self.view assumes the base view is doing the launching, if not you might need self.view.superview.center etc.
Hope this help you.

how to change first loading view

I got this code on didFinishLaunching but I don't know how to customize it to open up menuViewController from when I open the app.
- (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];
// At the end of applicationDidFinishLaunching, right before
// the return YES
[[GCTurnBasedMatchHelper sharedInstance] authenticateLocalUser];
return YES;
}
hope someone can help me out
Change the following line
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
to
self.viewController = [[[menuViewController alloc] initWithNibName:#"menuViewController" bundle:nil] autorelease];
The line you need to change is
self.window.rootViewController = self.viewController;
To
MenuViewController *menuViewController = [[[MenuViewController alloc] init] autorelease];
self.window.rootViewController = menuViewController;
This is assuming you have a custom view controller called MenuViewController
Create the object of menuViewController assign this controller to rootViewController.
self.viewController = [[[MenuViewController alloc] initWithNibName:#"menuViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

Push view not pushing UIViewController from single view based application

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;
}

About releasing objects created by me

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.