0

Hi I've seen an answer to this question:

How to pass a value from one view to another view

And I'm having some trouble. I've stored a string in the header file of the AppDelegate.

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    NSString *commString;
}

@property (strong, nonatomic) UIWindow *window;

@end

I now need to access this and change it in one view. Then display it in another view. The answer on the previous page explains this briefly, but I'm having trouble with the second part of the answer. It wont let me do:

AppDelegate.commString = myString; //mystring being an NSString

Any ideas please?

Thanks

Community
  • 1
  • 1
dev6546
  • 1,412
  • 7
  • 21
  • 40

1 Answers1

4

The problem is twofold. First, that you are trying to access an ivar on a class, and second that it is a class instead of an instance. [[UIApplication sharedApplication] delegate]; returns a valid instance of the delegate class as a singleton for easy access in multiple locations, but you need to declare the ivar as an @property, or else risk using the (extremely unstable) struct access operator.

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) NSString *commString; //@synthesize this
@property (strong, nonatomic) UIWindow *window;

@end


AppDelegate *del = [[UIApplication sharedApplication] delegate];
del.commString = myString;
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • Thanks, I'm learning all the time thanks to this site. @synth allows accesors and mutators yeah? Would I synthesize it in both views I use it in or the AppDelegate.m? – dev6546 Aug 11 '12 at 14:43
  • You only syntehsize in the .m file named the same as the .h file you declared them in. I.e., you only synthesize the window and commString in the AppDelegate.m file. – CodaFi Aug 11 '12 at 14:46