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

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.

Related

Printing vc1 NSString from vc2

I want to print out vc1.string1 from vc2.
Currently console's showing:
vc1.string1 (null)
When I was not using storyboard I accessed vc1 variable like this:
AppDelegate *appDelegate = [(AppDelegate *)[UIApplication sharedApplication]delegate];
NSLog(#"vc1.string1 %#", appDelegate.viewController.string1);
But I don't know how to access vc1.string when I'm using storyboard.
Help please thanks.
P.S.
Here's the link of my project: http://dl.dropbox.com/u/12439052/AccessDiffClass.zip
//ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
NSString *string1;
}
#property (nonatomic, strong) NSString *string1;
#end
#import "ViewController.h"
#implementation ViewController
#synthesize string1;
-(void)viewDidLoad {
string1 = #"String One";
NSLog(#"string1 %#", string1);
}
#end
VC2:
//ViewController2.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface ViewController2 : UIViewController {
ViewController *vc1;
}
#property (nonatomic, strong) ViewController *vc1;
#end
#import "ViewController2.h"
#import "ViewController.h"
#implementation ViewController2
#synthesize vc1;
-(void)viewDidLoad {
NSLog(#"vc1.string1 %#", vc1.string1);
}
#end
I downloaded your project and added this bit of code to your ViewController.m file:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog( #"preparing for segue" );
ViewController2 * vc2 = [segue destinationViewController];
vc2.vc1 = self;
}
And this got things appearing as you were hoping for in my simulator console.
Now, this is definitely not the best thing to do here. In the ARC world, I don't know if vc1 is retained or if we're leaking or whatever. It would be much smarter for you to give your ViewController2 class a NSString * property that gets set in the prepareForSegue method. And also give an identifier to your segue.
Here is another StackOverflow question that talks about prepareForSegue a bit more (and somewhat more detailed).
made a mistake last time try this.
Key Code:
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
vc1 = appDelegate.viewController;
NSLog(#"string1 %#", vc1.string1);
http://dl.dropbox.com/u/12439052/passingValue.zip

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

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!

how to use pagecontrol globally?

In my application i want to use instance of pagecontrol in more then 3 views..so,i want to declare,#property and #synthesize should be in appDelegate..Please give some ideas to do this...
Thanks,
Mano
I never used pagecontrol, but in principle, your code would look like:
in YourAppDelegate .h
#interface YourAppDelegate : NSObject <UIApplicationDelegate> {
...
UIPageControl *pageControl;
...
}
#property (nonatomic, retain) IBOutlet ScrollingViewController *viewController;
YourAppDelegate.m:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
pageControl = [[UIPageControl alloc] init]; // or whatever is needed to set up pagecontrol
}
in one of your views:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
//and then access the variable by appDelegate.variable