Error While referencing AppDelegate - iphone

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!

Related

Null value when accessing variable in other classes (Combined Navigation & Tab Controller)

I have a navigation controller residing inside a tab bar controller and whenever I try to access a class from a class within the navigation controller all my values return (null).
This is how I'm trying to do it.
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> {
NSString *searchQueryA;
}
#property (strong, nonatomic) NSString *searchQueryA;
ThirdViewController.h
#import "MasterViewController.h"
#import "AppDelegate.h"
#class MasterViewController;
#interface ThirdViewController : UIViewController {
code
}
#property (strong, retain) MasterViewController *masterViewController;
ThirdViewController.m
- (IBAction)showDetail:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
appDelegate.searchQueryA = _searchField.text;
masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
[self.navigationController pushViewController:masterViewController animated:YES];
}
MasterViewController.h
#import "AppDelegate.h"
#interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}
#property (nonatomic, strong) NSString *searchQueryM;
MasterViewController.m
AppDelegate *appDelegate = [[AppDelegate alloc] init];
searchQueryM = appDelegate.searchQueryA;
NSLog(#"%#", searchQueryM);
And in the log I can see that searchQueryM is (null). If I try to access the variable in AppDelegate from another class, that isn't involved with navigation controller, then it shows perfectly fine. What am I missing?
If you need to see more code I'd be happy to provide it.
EDIT:
For legibility I'll post code changes here:
I have the delegate in my AppDelegate.h
As Leonardo pointed out I only alloc'd and init'd my AppDelegate. I changed that snippet to this:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
searchQueryM = appDelegate.searchQueryA;
but still no go as searchQueryM still is (null).
This is what I do with searchQueryM
MasterViewController.h
#interface MasterViewController : UITableViewController
{
NSString *searchQueryM;
}
#property (nonatomic, strong) NSString *searchQueryM;
MasterViewController.m
#synthesize searchQueryM;
I'm fairly new to Objective-C (as well as OO-programming) and should probably read a book on it, but it seems to me like there isn't a lot more to it than that. Do correct me if I'm wrong.
EDIT 2
ThirdViewController.h
#interface ThirdViewController : UIViewController {
UITextField *_searchField;
}
#property (nonatomic, strong) IBOutlet UITextField *searchField;
ThirdViewController.m
#synthesize searchField = _searchField;
...
- (IBAction)showDetail:(id)sender {
_code_
NSLog(#"%#", searchField.text);
_code_
If i type in "asd" in the searchField textfield and output it with the log I get "asd".
}
Why are you alloc init your AppDelegate ?
The AppDelegate should be accessed with:
[[UIApplication sharedApplication] delegate]
We should see how you normally initialize searchQueryM, you are getting null, probably because the AppDelegate get only allocated and init, but the logic that initialize its properties never gets called.

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.

Xcode - warning problem

I have a navigation controller that utilizes an if statement to switch between the different views, and when i run it it comes up with a warning on the line:
ROSS_APP_7AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
warning: type 'id ' does not conform to the 'UITabBarControllerDelegate' protocol
I'd like some help on how to get rid of this warning.
Here is the whole if statement:
if(indexPath.row == 0)
{
MapDetailController *mapD = [[MapDetailController alloc] initWithNibName:#"MapDetailController" bundle:nil];
self.mapDetailController = mapD;
[mapD release];
mapDetailController.title = [NSString stringWithFormat:#"%#", [moreArray objectAtIndex:row]];
ROSS_APP_7AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.moreNavController pushViewController:mapDetailController animated:YES];
}
Thanks
EDIT:
Here is what my AppDelegate looks like (response to answer #2)
#import <UIKit/UIKit.h>
#class MoreNavController;
#interface ROSS_APP_7AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
IBOutlet UITabBarController *tabBarController;
IBOutlet MoreNavController *moreNavController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet MoreNavController *moreNavController;
#end
You probably forgot to have your app delegate conform to the UITabBarControllerDelegate protocol.
To implement it, your appDelegate header should look like this (the #interface declaration is the relevant line):
#import ...
#interface ROSS_APP_7AppDelegate : AppDelegate_Shared <UITabBarControllerDelegate>
{
....
}
#property(nonatomic, retain) .....
#end
You might be using AppDelegate_Shared / AppDelegate_iPhone / AppDelegate_iPad so bear in mind that the above example considers a shared app delegate
EDIT:
After seeing your comment,
Try replacing:
ROSS_APP_7AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
With this:
ROSS_APP_7AppDelegate *delegate = (ROSS_APP_7AppDelegate*)[[UIApplication sharedApplication] delegate];
Does typecasting the return like this get rid of your warning?
Have you tried casting the delegate as in:
(id)[[UIApplication sharedApplication] delegate];
???

data between Views UIApplication

i want to share data between views...
i have the appdelegate of tabbar application:
myappdelegate.h
#import <UIKit/UIKit.h>
#interface myappdelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
NSString *result;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (copy , readwrite) NSString *result;
#end
if i want to call with this command, there is the hint: "may not respond"....
myappdelegate *dataCenter = [(myappdelegate *)[UIApplication sharedApplication] delegate]; <<may not respond
dataCenter.result = #"msg";
result_view *resultView = [[result_view alloc] initWithNibName:#"result_view" bundle:nil];
[self.navigationController pushViewController:resultView animated:YES];
[resultView release];
result_view.m
- (void)viewDidLoad
{
myappdelegate *dataCenter = (myappdelegate*)[[UIApplication sharedApplication]delegate];
[label setText:dataCenter.result];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
the program crashes...
Your code is saying that the sharedApplication is of the class myappdelegate, which indeed does not respond to delegate. Do this:
(myappdelegate *)[[UIApplication sharedApplication] delegate];
to remove the warning.
Due to Objective-C's runtime messaging, your current (warning-generating) code won't crash the app. The crash lies somewhere else.
Your first line should be
myappdelegate *dataCenter = (myappdelegate *)[[UIApplication sharedApplication] delegate];
As for the second line, I can't tell what you expect to happen. You don't have a result_array property on your myappdelegate class, so of course you can't set that property.
If you were trying to set the result property, you should have written
dataCenter.result = #"msg";

Sharing Data between XIB's

I'm following the James Brannan tutorial's, and im trying to share data between some xbis. No luck.
I have 2 xib's. The first, simple button and textfield. The second, just a label, to show the result of the first xib textfield.
So, i dont know what im doing wrong. Im using NSObject like in tutorial.
SharedData.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface SharedData : NSObject {
NSString *MeuNome;
}
#property (nonatomic, retain) NSString *MeuNome;
#end
SharedData.m
#import "SharedData.h"
#implementation SharedData
#synthesize MeuNome;
- (void) dealloc {
self.MeuNome = nil;
[super dealloc];
}
#end
FirstStepViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "SharedData.h"
#interface FirstStepViewController : UIViewController {
IBOutlet SharedData *sharedData;
IBOutlet UITextField *campoNome;
}
#property (nonatomic, retain) UITextField * campoNome;
#property (nonatomic, retain) SharedData *sharedData;
- (IBAction) takeNextStep: (id) sender;
#end
FirstStepViewController.m
#import "FirstStepViewController.h"
#import "SecondStepViewController.h"
#import "LuconeAppDelegate.h"
#implementation FirstStepViewController
#synthesize campoNome, sharedData;
- (IBAction) takeNextStep : (id) sender{
// declaracao de shared data
[sender resignFirstResponder];
self.sharedData.MeuNome = self.campoNome.text;
// faz animacao para proximo slide
SecondStepViewController *varSecondViewController = [[SecondStepViewController
alloc] initWithNibName:#"SecondStepViewController" bundle:nil ];
[self.navigationController pushViewController:varSecondViewController
animated: YES];
[self navigationController].navigationBarHidden = NO;
}
- (void)viewDidLoad {
[self navigationController].navigationBarHidden = YES;
[super viewDidLoad];
}
- (void)dealloc {
self.sharedData = nil;
//self.campoNome = nil;
[super dealloc];
}
#end
SecondStepViewController.h
#import <UIKit/UIKit.h>
#import "SharedData.h"
#interface SecondStepViewController : UIViewController {
IBOutlet SharedData *sharedData;
IBOutlet UILabel *nome;
}
#property (nonatomic, retain) SharedData *sharedData;
#property (nonatomic, retain) UILabel *nome;
#end
SecondStepViewController.m
#import "SecondStepViewController.h"
#import "SharedData.h"
#implementation SecondStepViewController
#synthesize nome, sharedData;
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"step two";
self.nome.text = self.sharedData.MeuNome;
}
- (void)dealloc {
self.sharedData = nil;
[super dealloc];
}
#end
What's wrong?
Thanks!
There are three small problems with your code.
IBOutlet is only for controls that you put on your view using interface builder e.g. UIButtons, UILabels. So it is unnecessary for instances of SharedData to be IBOutlets. Furthermore if you create your interface programmatically then using IBOutlet is again unnecessary.
You Declare that sharedData is an instance of SharedData class but you do not instantiate it. in your FirstStepViewController.m before you set any of sharedData's properties you should add the following code:
sharedData = [[sharedData alloc] init];
after that you can do:
self.sharedData.MeuNome = self.campoNome.text;
if you omit the "self." the code should work just as fine.
Finally before pushing the second view controller to the navigation stack you have to assign the sharedData object in your first view controller to sharedData in your second view controller.
in your FirstStepViewController.m add:
[varSecondViewController sharedData] = [self sharedData];
before:
[self.navigationController pushViewController:varSecondViewController
animated: YES];
Finally make sure you have connected all your outlets correctly in interface builder and everything should run perfectly then :)