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:
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.