App Crashes Loading View Controllers - iphone

I have the following code in my AppDelegate.h file:
#class mainViewController;
#class AboutViewController;
#interface iSearchAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
mainViewController *viewController;
AboutViewController *aboutController;
UINavigationController *nav;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet mainViewController *viewController;
#property (nonatomic, retain) IBOutlet AboutViewController *aboutController;
#property (nonatomic, retain) IBOutlet UINavigationController *nav;
[...IBActions declared here...]
#end
Then, in my .m file:
#implementation iSearchAppDelegate
#synthesize window;
#synthesize viewController, aboutController, settingsData, nav, engines;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:nav.view];
[window addSubview:aboutController.view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
-(IBAction)switchToHome{
[window bringSubviewToFront:viewController.view];
}
-(IBAction)switchToSettings{
[window bringSubviewToFront:nav.view];
}
-(IBAction)switchToAbout{
[window bringSubviewToFront:aboutController.view];
}
- (void)dealloc {
[viewController release];
[aboutController release];
[nav release];
[window release];
[super dealloc];
}
#end
Somehow, when I run the app, the main view presents itself fine... however, when I try to execute the actions to switch views, the app crashes with an EXC_BAD_ACCESS.
So, I think it has something to do with memory management, but I'm not quite sure.
Thanks for any help in advance.
Link to screenshots of code is here: link...
SOLVED: I fixed the issue by taking out the IBActions and making them into regular methods... apparently, XCode doesn't like it when you put IBActions in an AppDelegate.

... message sent to deallocated instance ...
If it is memory management, my first step would be to enable NSZombie and discover what was being messaged after being dealloc'ed. Two obvious things I can think of:
Uninitialised property/variable.
De-allocated (non-retained) property
Have your controls in interface builder been connected to the IBActions?

Somewhere in your code, you are invoking [iSearchAppDelegate performSelector:withObject:withObject:]. You haven't shown that code here but that's likely where the problem is.

Related

in App Delegate do I need to release my "window" and "navigationController"?

In App Delegate do I:
need to release my "window" and "navigationController"? and
where abouts should I release it out of (a) applicationDidReceiveMemoryWarning and (b) dealloc?
Code Listing
#interface weekendviewerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#implementation weekendviewerAppDelegate
#synthesize window;
#synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
self.window.rootViewController = self.navigationController;
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
return YES;
}
.
.
As Bolt clock commented you need to add a dealloc method in appDelegate class.
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
Well #greg if you ever release your window or navigationController in applicationDidReceiveMemoryWarning dont u think your application will crash when ur application receives memory warning.
As #Bolt and #ishu said you need to release it in dealloc methods only.
Also in applicationDidReceiveMemoryWarning method you can release those class variables which are not going to be used after some point of time, as releasing them may cause your app to crash.
So choose wisely what variables are not important that might cause your application to crash or stop your app from working properly.

iPhone Loading Different Views

I have to xibs, and I want to load which one the user wants in the settings. I'm just working on actually loading them and how would this been done.
This is the function in the delegate which I think would be where this happens.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
This is the delegate header file.
#import <UIKit/UIKit.h>
#class myAppViewController;
#interface myAppAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
myAppViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet myAppViewController *viewController;
#end
So lets say I have a xib created called modernTheme, how would I load that or the myAppViewController. If someone could do this in a generic if statement, that would be great.
This looks like your viewController is being created 'in Interface Builder'. Instead of that you could create the ViewControllers yourself and add them dynamically. Your application:didFinishLaunchingWithOptions: could look something like this:
UIViewController *viewController;
if (showModernTheme) { // from your configuration
viewController = [[YourViewControllerA alloc] initWith…];
} else {
viewController = [[YourViewControllerB alloc] initWith…];
}
// assuming YourViewControllerA + B are inheriting from UIViewController
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
Hope that helps
–f

iPhone navigationController is not displayed

I have a very strange behaviour of my navigation view. What I want is, that from my main view, the user can touch a button, which leads him to the view with the application settings.
Here is the code, responsible for the navigation:
AppDelegate.h
#interface AppDelegate : NSObject {
UIWindow *window;
ViewController *viewController; // My Main View Controller
UINavigationController *navigationController; }
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
AppDelegate.m
#synthesize viewController;
#synthesize window;
#synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:viewController.view];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
viewController.h
#import
#import "optionsViewController.h" // the 'settings' view controller
#class AppDelegate;
#interface ViewController : UIViewController {
AppDelegate *appDelegate;
viewController.m
- (IBAction)showOptionsViewController:(UIBarButtonItem *)sender {
// optionsController.theSubjectID = self.theSubjectID;
// [self presentModalViewController:self.optionsController animated:YES];
optionsViewController *optionsController= [[optionsViewController alloc] initWithNibName:#"optionsViewController" bundle:nil];
optionsController.theSubjectID = self.theSubjectID;
[self.navigationController pushViewController:optionsController animated:YES];
[optionsController release];
}
My optionsController is a 'normal' UIViewController. As you see did I change the load of the optionsController from modal to navigation. Could it be, that I missed something here?
Thanks for any hints in advance.
Cheers, René
Have you connected it up in Interface Builder if not you need to alloc/init it before you add it as a subview?

EXC_BAD_ACCESS in tableview application

This is my first iPhone application and it's based on a top-level tableview. Selections of rows either go to another tableview or to a view. The application runs OK on the simulator but when ported to my iPhone it fails with a EXC_BAD_ACCESS error. This happens while my splash screen is being displayed. NSLog indicates that the program processes in appDelegate.m:
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
but then it just fails. The code never seems to reach the viewDidLoad in my RootViewController.
I'm sure that I've got the RootViewController and appDelegates mixed up somehow but cannot figure out exactly what's wrong. I've attached the beginning code of my RootViewController, appDelegate - any help appreciated.
RootViewController.h code....
#interface RootViewController : UITableViewController {
TyresViewController *tyresController;
EngineSpecViewController *engineSpecController;
CarbonTaxBandViewController *carbonTaxBandController;
TyreSpecificationsViewController *tyreSpecificationsController;
FuelConsumptionandEmissionsViewController *fuelConsumptionandEmissionsController;
CompanyCarTaxBandViewController *companyCarTaxBandController;
CarbonCalculatorViewController *carbonCalculatorController;
ReminderViewController *reminderController;
//NSString *selectedSpecification;
NSArray *listOfItems;
}
RootViewController.m code ......
#import "RootViewController.h"
#implementation RootViewController
#synthesize listOfItems;
//#synthesize selectedSpecification;
#synthesize carbonTaxBandController;
#synthesize engineSpecController;
#synthesize tyreSpecificationsController;
#synthesize tyresController;
#synthesize fuelConsumptionandEmissionsController;
#synthesize companyCarTaxBandController;
#synthesize carbonCalculatorController;
#synthesize reminderController;
appDelegate.h code.....
#interface MyCar3AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
appDelegate.m code .....
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
Just a thought, in your main Info.plist file there should be an entry for Main nib file base name. This refers to the nib that will be loaded when your app starts. The simulator is not case sensitive, but the device is. Check the case of the value of your main nib.

Where is the UIWindow instantiated in an iPhone app?

When you create an application from the "View-Based" template in the iPhoneSDK the following code is generated. I basically understand what is happening here, but I do not see where window and viewController are instantiated. Any Help?
#class jojojViewController;
#interface jojojAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
jojojViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet jojojViewController *viewController;
#end
===============================================
#implementation Test6AppDelegate
#synthesize window,mainView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
#end
They come from the MainWindow.xib (or similar) file in your project.
This is the file that in your info.plist is set as the application window. When your application starts this xib is loaded and the viewcontroller and window are unarchived and loaded.
If you look in MainWindow.xib, the window and viewcontroller are assigned to your AppDelegate's window and viewController outlets, which instantiates them when the nib is loaded (right click on the AppDelegate to see it).