Creating an iPhone Application in Xcode 4.1.1 - Program received signal "SIGABRT" - iphone

I'm new to creating iPhone Applications and have just started with Xcode and am getting the following errors; I have used this code previously on both the simulator & an iPod touch and it worked fine for both but for some reason lately it will not allow me to go from one page to another without giving me the "SIGABRT" error.
Basically in my application I need to go from one page to another several times but it will not work....can anyone help with this please?
This is the code it seems unhappy with (it complies & builds successfully):
1 #import <UIKit/UIKit.h>
2
3 int main(int argc, char *argv[])
4 {
5 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
6 int retVal = UIApplicationMain(argc, argv, nil, nil);
7 [pool release];
8 return retVal;
9 }
It stops on the int retVal line (6th line).
When debugging it gave the error:
2011-09-09 15:33:59.029 TruckFile[1072:b603] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[< UIApplication 0x6044600> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key window.'
Can anyone help me with this please?
Thank you in advance!
========================================
Code files:
========================================
Main page (.h)
#import <UIKit/UIKit.h>
#import "ViewTwoController.h"
#import "TruckFileAppDelegate.h"
#interface TruckFileAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewTwoController *viewTwoContoller;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, retain) ViewTwoController *viewTwoController;
-(IBAction)switchPage:(id)sender;
#end
================================================
Main page (.m)
#import "TruckFileAppDelegate.h"
#import "ViewTwoController.h"
#implementation TruckFileAppDelegate
#synthesize window;
#synthesize navigationController;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize managedObjectModel = __managedObjectModel;
#synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
#synthesize viewTwoController;
-(IBAction)switchPage:(id)sender
{
if(self.viewTwoController == nil)
{
ViewTwoController *viewTwo = [[ViewTwoController alloc]
initWithNibName:#"ViewTwoController" bundle:[NSBundle mainBundle]];
self.viewTwoController = viewTwo;
[viewTwo release];
}
[self.navigationController pushViewController:self.viewTwoController animated:YES];
}
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
#end
====================================
Page 2 (.h)
#import <UIKit/UIKit.h>
#interface ViewTwoController : UIViewController {
}
#end
=====================================
Page 2 (.m)
#import "ViewTwoController.h"
#implementation ViewTwoController
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
#end

The mainWindow.xib is trying to connect it's UIWindow instance with your UIApplicationDelegate's and it can't find it (that's why it complains it can't set the value for an UNDEFINED key).
Check your UIApplicationDelegate class has a UIWindow iVar, a property in its .h and it's correctly synthesized in its .m.
EDIT
Who's calling the method switchPage: on UIApplication? If you are, you're calling it on the wrong object. Instead of doing
[[UIApplication sharedApplication] switchPage:xxx];
You should do:
[[[UIApplication sharedApplication] delegate] switchPage:xxx];
Since the method switchPage: is defined in the UIApplicationDelegate class.

Your code fails at
[UIApplication switchPage:]:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIApplication switchPage:]: unrecognized selector sent to instance 0x600beb0'
This means that in your switchPage method, you are giving an action to an unknown identifier, maybe check the object identifier name, and check if you have initialized it in your .h file.

Related

how to get its own view controller inside itself?

#interface RecentPhotosViewController () <PhotoViewControllerDelegate>
- (void)viewDidLoad
{
[super viewDidLoad];
[[self.tabBarController.viewControllers objectAtIndex:1] setDelegate:self];
}
The RecentPhotosViewController is a tableviewcontroller which implements a delegate.
I want to set self(RecentPhotosViewController) as the delegate in vieDidLoad(), but when i tried to type:self.setDelegate it turned out self doesn't have this setDelegate method, then i tried this:[[self.tabBarController.viewControllers objectAtIndex:1] setDelegate:self];(this tableview is one of the viewcontrollers of the tabBarControllers.
Then i got an error:[RecentPhotosViewController setDelegate:]: unrecognized selector sent to instance 0xc939960. I don't know how to fix this.
Do this:
#interface RecentPhotosViewController
//etc.
#property (nonatomic, assign) id delegate;
#end
#implementation RecentPhotosViewController
// etc.
#synthesize delegate;
#end

iOs simple hello, name not working

I have recently started to learn Obj-C and Iphone development basically through Lynda.com iOs SDK essentials course. But it was written for Xcode 3.x and I have 4.0.x installed, so things are different.
Basically, I take example from there and it just doesn't work for me and I can't figure it out being a noob to it.
//basicViewController.h
#import <UIKit/UIKit.h>
#interface basicViewController : UIViewController {
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
- (IBAction) doSomething;
#end
And my basicViewController.m
#import "basicViewController.h"
#implementation basicViewController
#synthesize txtName;
#synthesize lblMessage;
- (IBAction) doSomething
{
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",txtName.text];
[lblMessage setText:msg];
[msg release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I haven't changed basicAppDelegate.h and .m from what they were created like. I think I will post them anyway.
//
// basicAppDelegate.h
#import <UIKit/UIKit.h>
#class basicViewController;
#interface basicAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
basicViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet basicViewController *viewController;
#end
//
// basicAppDelegate.m
#import "basicAppDelegate.h"
#import "basicViewController.h"
#implementation basicAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
And here's my main.m:
//
// main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Also, on the Interface Builder window I have linked through File's Owner: label to lblMessage, TextField to txtName and doSomething to Button.
Now, the problem is: actually, the thing kinda works, only if I use the popup keyboard, not my physical one. And only if I type less than 3-4-5 symbols(differs sometimes).
If I use my keyboard, not the popping one - it gives me Thread 1 received signal EXC_BAD_ACCESS.
Same if I type too many symbols.
And I don't see much in All Output - just some usual stuff, the only suspicious line is:
warning:unable to compile regular expression "dyld"
Current language: auto; currently objective-c
(gdb)
So, guys, any kind help is appreciated.
First, there didn't change something since Xcode 3 except the interface.
Try to set a breakpoint and look where you app is crashing...
Try to delete the IBOutlets and create it new...
Try to create a new app...
If all not works, take your Mac and throw it outside a window, bring it back to Apple. (or install Xcode again --> much cheaper..)
I had the exact same problem and spent AGES looking for the solution. I found out how to make it work and I've made a tutorial on how to make the Hello, Name application on XCode. Take a look and see if it helps. I think if you follow the steps one by one you should have your app running in no time.
http://techtalktone.wordpress.com/2011/11/26/hello-world/
Hope this helps ;)

Using AppDelegate for global readonly data

I have a project on iPhone with iOS4.
A instance variable of app delegate is a dictionary with global readonly data loaded from a plist when app starts.
CalculatorAppDelegate.h
#import <UIKit/UIKit.h>
#class MainViewController;
#interface CalculatorAppDelegate : NSObject <UIApplicationDelegate> {
NSDictionary *RGBSpacesDictionary;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain, readonly) NSDictionary *RGBSpacesDictionary;
#property (nonatomic, retain) IBOutlet MainViewController *mainViewController;
#end
CalculatorAppDelegate.m
#import "CalculatorAppDelegate.h"
#import "MainViewController.h"
#implementation CalculatorAppDelegate
#synthesize mainViewController=_mainViewController;
#synthesize RGBSpacesDictionary;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// load plist
NSString* plistPath1 = [[NSBundle mainBundle] pathForResource:#"RGBSpaces" ofType:#"plist"];
RGBSpacesDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath1];
etc.
}
Then in MainViewController i am able to successfully reading the dictionary in viewDidLoad
MainViewController.h
#class CalculatorAppDelegate;
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
CalculatorAppDelegate *appDelegate;
}
#property (nonatomic, retain) CalculatorAppDelegate *appDelegate;
etc.
}
MainViewCOntroller.m
#import "CalculatorAppDelegate.h"
#implementation MainViewController
#synthesize appDelegate;
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = [[UIApplication sharedApplication] delegate];
RGBSpacesCount = (int) [appDelegate.RGBSpacesDictionary count];
}
In viewDidLoad it is all OK, I can read my dictionary as appDelegate.REGSpaceDictionary.
The problem is with another method of MainVievController called when a button is pressed
- (IBAction) RGBSpaceButtonPressed {
NSLog(#"appDelegate.RGBSpacesDictionary %#", appDelegate.RGBSpacesDictionary);
etc.
}
At this time calling the dictionary (for example with a NSLog) return in a crash.
Can someone help me? Thank you.
In this line
RGBSpacesDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath1];
you are assigning an autoreleased object straight to the ivar so there is no guarantee how long it will stay around for. You should be assigning a non autoreleased object or going through the setter
// Going through the setter
self.RGBSpacesDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath1];
// OR
// Non assigning a non autoreleased object
RGBSpacesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
To use the setter you would have to redeclare the property in an extension at the top of the app delegate's .m file like this
#interface CalculatorAppDelegate ()
#property (nonatomic, retain, readwrite) NSDictionary *RGBSpacesDictionary;
#end
...
The rest of your implementation
Try to retain the dictionary in app delegate. It must be deallocated at some point, because you get an autoreleased one and you didn't use the property to set it.
// here you must retain the dictionary
[[NSDictionary dictionaryWithContentsOfFile:plistPath1] retain];
Of course don't forget to release it later in dealloc.

Error While referencing AppDelegate

I have an App Delegate and a 3 view controllers in my project. I have a variable(a NSMutable Array) in my App Delegate which I want to access from my view controllers. So I decided to create a pointer to my App Delegate and access the variables.
Here is my code:
iSolveMathAppDelegate.h
#import <UIKit/UIKit.h>
#interface iSolveMathAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
NSMutableArray *tmpArray;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) NSMutableArray *tmpArray; // variable I want to access
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
iSolveMathAppDelegate.m
#import "iSolveMathAppDelegate.h"
#implementation iSolveMathAppDelegate
#synthesize window;
#synthesize tabBarController;
#synthesize tmpArray;
...
- (void)dealloc {
[tabBarController release];
[window release];
[tmpArray release];
[super dealloc];
}
#end
The view controller class from which I want to access the tmpArray.
referenceViewController.h
#import <UIKit/UIKit.h>
#class iSolveMathAppDelegate;
#interface referenceViewController : UITableViewController {
NSMutableArray *equationTypes;
iSolveMathAppDelegate *data;
}
#property(nonatomic, retain) NSMutableArray *equationTypes;
#property(nonatomic, retain) iSolveMathAppDelegate *data;
#end
And finally referenceViewController.m
#import "referenceViewController.h"
#implementation referenceViewController
#synthesize equationTypes, data;
data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate];
//says that initializer element is not constant...ERROR!
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"equationTemplates"ofType:#"plist"];
data.tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.equationTypes = data.tmpArray;
[data.tmpArray release]; // obviously none of these work, as data is not set.
}
- (void)dealloc {
[super dealloc];
[equationTypes release];
[data release];
}
#end
So anyway at the line data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; the compiler says that the initializer element is not constant.
I have scrouged the web for answers, and for all it seems to work...but no dice for me :( Can you please advice me on where I have gone wrong? I am using XCode 3.2 and iOS SDK 3....maybe the SDK is the problem.
Thank You
That line of code isn't in a method or function, so the compiler is treating it as the definition of a compile-time constant or static/global variable. Those need constant values for initialization.
You should put the assignment of data within a method. A good place would be -viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate];
...
}
I figured out the struct and union problem. All I had to do was change #class iSolveAppDelegate to #import "iSolveAppDelegate.h" in my referenceViewController.h file. Thanks Jonathan for your help!

App Crashes Loading View Controllers

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.