using appDelegate to share data - getting "request for member in __ not in structure or union" - objective-c

I've got a simple example where i have a delegate that has a string, along with a property to expose it.
myAppDelegate.h
#import <UIKit/UIKit.h>
#interface myAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *rootController;
NSString* myText;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#property (nonatomic, retain) NSString *myText;
#end
myAppDelegate.m
#import "myAppDelegate.h"
#implementation myAppDelegate
#synthesize window;
#synthesize rootController;
#synthesize myText;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[rootController release];
[window release];
[super dealloc];
}
#end
So in my main view controller, I try something like this:
myAppDelegate* ad = (myAppDelegate*)[UIApplication sharedApplication].delegate;
ad.myText = #"blah";
and I get: Request for member 'myText' in something not a structure or union
does anyone know why this is happening?

Have you tried using setter instead of dot notation?
[ad setMyText:#"blah"];

Related

How do I fix setManagedObjectContext error?

Whenever I run my app it always crashes and gives me this error:
2014-08-10 12:50:58.802 EIC Scanner[2184:133860] -[UITabBarController
setManagedObjectContext:]: unrecognized selector sent to instance
0x7ae7b9f0 (lldb)
I have an exception breakpoint so it leads me to this line of code:
rootView.managedObjectContext = self.managedObjectContext;
AppDelegate.h:
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
Part of AppDelegate.m:
#import "AppDelegate.h"
#import "NotesList.h"
#import <CoreData/CoreData.h>
#implementation AppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// IMPORTANT: Pass the managedObjectContext to the root view controller
NotesList *rootView = (NotesList *)_window.rootViewController;
rootView.managedObjectContext = self.managedObjectContext;
return YES;
}
As requested NotesList.h:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface NotesList : UIViewController <
UITableViewDelegate,
UISearchBarDelegate, UISearchDisplayDelegate
>
// CoreData properties *******
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) NSMutableArray *notesArray;
//UI VIEWS ***********
#property (retain, nonatomic) IBOutlet UIView *topView;
//TableView ********
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#end
If you know how to fix this, please comment on how. Thanks for the help!

Not Properly Passing Data Between Views

I'm trying to send text generated by the user from the AuthenticationViewController to MainProfileViewController. I'm not receiving an error report. The label just doesn't even appear in the MainProfileViewController. The outlets are correctly hooked up. Thanks for the help!
#import <UIKit/UIKit.h>
#class MainProfileViewController;
#interface AuthenticationViewController : UIViewController
{
MainProfileViewController *mainProfileViewController;
}
#property (retain, nonatomic) IBOutlet UITextField *usernameTextField;
#end
#import "AuthenticationViewController.h"
#import "MainProfileViewController.h"
#interface AuthenticationViewController ()
#end
#implementation AuthenticationViewController
#synthesize usernameTextField;
- (IBAction)createAccount: (id)sender {
{
mainProfileViewController = [[MainProfileViewController alloc] init];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
}
#end
#import <UIKit/UIKit.h>
#import "AuthenticationViewController.h"
#interface MainProfileViewController : UIViewController
{
UITextField *userText;
}
#property (retain, nonatomic) IBOutlet UILabel *resultLabel;
#property (retain, nonatomic) IBOutlet UITextField *userText;
#property (retain, nonatomic) NSString *textPass;
#end
#import "MainProfileViewController.h"
#import "AuthenticationViewController.h"
#interface MainProfileViewController ()
#end
#implementation MainProfileViewController
#synthesize resultLabel, userText, textPass;
- (void)viewDidLoad
{
userText.text = textPass;
[super viewDidLoad];
}
#end
OK, there's a few things you're doing wrong.
First off, you're instantiating a local MainProfileViewController instead of instantiating the one that you have an ivar pointing to.
The second thing that you're doing wrong is trying to send over the view from the AuthenticationViewController to the MainProfileViewController. You shouldn't do that. Instead, pass the text itself; otherwise you are just overwriting pointers, and nothing will show up.
- (IBAction)createAccount: (id)sender {
// DON'T CREATE A LOCAL VARIABLE
// MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
mainProfileViewController = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// DON'T TRY SENDING THE VIEW OVER
// mainProfileViewController.userText = usernameTextField;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
edit: I suppose it's not required that you have an ivar to the new view controller you want to present... But the point is that you cannot instantiate one, and then set the parameter on the one you did not instantiate (it will be nil).
Check your story board and see if you connected the Outlets properly.
There should be an IBOutlet object in your view controller that you should connect in story board.
Also check if you connected the correct view controller class to the view controller in storyboard
#import <UIKit/UIKit.h>
#class MainProfileViewController;
#interface AuthenticationViewController : UIViewController
{
MainProfileViewController *mainProfileViewController;
}
#property (retain, nonatomic) IBOutlet UITextField *usernameTextField;
#end
#import "AuthenticationViewController.h"
#import "MainProfileViewController.h"
#interface AuthenticationViewController ()
#end
#implementation AuthenticationViewController
#synthesize usernameTextField;
- (IBAction)createAccount: (id)sender {
{
MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.textpass = usernameTextField.text;
[self presentViewController:controller animated:YES completion:nil];
}
}
#end
#import <UIKit/UIKit.h>
#import "AuthenticationViewController.h"
#interface MainProfileViewController : UIViewController
{
UITextField *userText;
}
#property (retain, nonatomic) IBOutlet UILabel *resultLabel;
#property (retain, nonatomic) IBOutlet UITextField *userText;
#property (retain, nonatomic) NSString *textpass;
#end
#import "MainProfileViewController.h"
#import "AuthenticationViewController.h"
#interface MainProfileViewController ()
#end
#implementation MainProfileViewController
#synthesize resultLabel, userText;
- (void)viewDidLoad {
usertext.text=textpass;
}
#end
You need to change your code on this part...
- (IBAction)createAccount: (id)sender
{
mainProfileViewController = [[MainProfileViewController alloc] init];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
Happy Coding...

Passing map-data between ViewController

I nearly followed this post. The viewcontroller will push via storyboard. Here the relevant code:
ViewController.m:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
MapPoint.h:
#interface MapPoint : NSObject <MKAnnotation>
{
NSString *_title;
NSString *_subtitle;
CLLocationCoordinate2D _coordinate;
UIImage *_storeImage;
}
#property (copy) NSString *title;
#property (copy) NSString *subtitle;
#property (nonatomic, readonly) UIImage *storeImage;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString*)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate storeImage:(UIImage*)storeImage;
and DetailViewController:
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
I think my mistake is in the calloutAccessoryControlTapped method but I dont know what is the real reason for this issue.
If you use a storyboard, can use prepareForSegue method and set within a class similarly as you have made.
I post a portion of code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"Brs"]){
NSLog(#"segue %# ",[segue identifier]);
[segue.destinationViewController setPath:[ris_xml objectAtIndex:index_post-1] ];
}
}
In this example I set attribute "Path" of next UIViewController only if his identifier is "Brs".
For use this method is need set UIviewController identifier into storyboard right panel.
Using this isn't need to instantiate new UIViewController if you have it in storyboard.
I solved it by thinking about loading sequence.
All what to do is create temporary #properties
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
#property (nonatomic) NSString *titleTMP; //added
#property (nonatomic) NSString *subtitleTMP; //added
and do
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
The DetailViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
titleLabel.text = titleTMP;
textView.text = subtitleTMP;
}

Use of undeclared identifier in AppDelegate?

In my AppDelegate implementation file i get an error for an undeclared identifier but it appears to be declared, what am i overlooking?
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
- (NSManagedObjectContext *)managedObjectContext
the error occurs at this line "Use of undeclared identifier "managedObjectContext"
In my AppDelegate header file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
#end

error: expected specifier-qualifier-list before 'GKPeerPickerController

I keep getting this message (in the title). Just take a quick look at my code if you want to see what I'm doing. I've just started implementing the Peer Picker, so I'm not completely done yet. I just need some advice/help in the first part. The error shows up in the .m file between the two #import statements, which means it has to be some wrong way that I've used the GKPeerPickerController in the header file.
Bluetooth_Ad_Hoc_NetworkAppDelegate.h
#import <UIKit/UIKit.h>
#class Bluetooth_Ad_Hoc_NetworkViewController;
#interface Bluetooth_Ad_Hoc_NetworkAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Bluetooth_Ad_Hoc_NetworkViewController *viewController;
GKPeerPickerController *picker;
GKSession *session;
IBOutlet UILabel *status;
NSData *data;
}
#property(nonatomic, retain)IBOutlet UILabel *status;
#property(nonatomic, retain)GKPeerPickerController *picker;
#property(nonatomic, retain)GKSession *session;
#property(nonatomic, retain)IBOutlet UIWindow *window;
#property(nonatomic, retain)IBOutlet Bluetooth_Ad_Hoc_NetworkViewController *viewController;
#end
Bluetooth_Ad_Hoc_NetworkAppDelegate.m
#import "Bluetooth_Ad_Hoc_NetworkAppDelegate.h"
#import "Bluetooth_Ad_Hoc_NetworkViewController.h"
#implementation Bluetooth_Ad_Hoc_NetworkAppDelegate
#synthesize status;
#synthesize picker;
#synthesize session;
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
// allocate and initialize data
data = [[NSData alloc] initWithBytes:&status length:sizeof(status)];
// Allocate and setup peer picker controller
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
}
- (void)dealloc {
[status release];
[viewController release];
[window release];
[super dealloc];
}
#end
Have you included this statement in the header file?
#import <GameKit/GameKit.h>
Also you need to include the GameKit framework.