0--
0--

Reputation: 221

In Objective-C, how does one share data among functions in a view controller?

Aiming for simplicity, how does one share data among functions within a view controller? Ideally, I would like to use a NSMutableDictionary, but my approach doesn’t seem to work (below):

In ViewController.m:

- (void) viewDidLoad{
...
  NSMutableDictionary * movietem = [[NSMutableDictionary alloc] init];
  [movieItem setValue:@“frozen” forKey:@“title” ];
  [movieItem setValue:@“PG” forKey:@“rating” ];
  [movieItem setValue:@“tom cruise” forKey:@“cast” ];  
....
}

-(IBAction) updateTitleBtn:(UIButton *)sender{
…
  [movieItem setValue:@"lion king" forKey:@"title"];
...
} 


-(IBAction) updateCastBtn:(UIButton *)sender{
…
  [movieItem setValue:@"forest whitaker" forKey:@"cast"];
...
} 

Concluding in an error: ‘Unknown receiver ‘movieItem’. Thanks for the input.

Upvotes: 0

Views: 128

Answers (4)

Exia
Exia

Reputation: 2551

movieitem is a local variable thus it can not be used in other methods.

Thus, a better way to share a variable between methods is to declare a property.

Try to add the code below into the head of your XXViewConttroller.m files.

@interface  XXViewConttroller()
@property(nonatomic,strong) NSMutableDictionary* movie;
@end

-(NSMutableDictionary*)movie{
   if(!_movie){
         _movie = [NSMutableDictionary dictionary];
   }
   return _movie;
}

In which I declared a "private property" in .m files and initialized the variable in the getter of the propery.

you can use self.movie to call the property in other part of your code.

Upvotes: 1

DevdDexter
DevdDexter

Reputation: 63

You have created your dictionary at -(void)viewDidLoad method in .m file.
In this sense your MutableDictionary is only accessible within viewDidLoad method. You can make your * movietem Dictionary public (accessible by all methods)
Here is a solution

Add this code below @interface ViewController : UIViewController in your ViewController.h file

 @property (nonatomic, strong) NSMutableDictionary *movietem; 
 // This line will create a public mutableDictionary which is accessible by all your methods in ViewController.m file

Now add this code below @implementation ViewController in ViewController.m file

@synthesize movietem; // This line will create getters and setters for movietem MutableDictionary


Now you can use your movietem dictionary anywhere you want
As per your code,

- (void) viewDidLoad {
...
      movietem = [[NSMutableDictionary alloc] init]; // allocating memory for movietem mutable Dictionary
      [movieItem setValue:@“frozen” forKey:@“title” ];
      [movieItem setValue:@“PG” forKey:@“rating” ];
      [movieItem setValue:@“tom cruise” forKey:@“cast” ];  
....
}
-(IBAction) updateTitleBtn:(UIButton *)sender {
...
[movieItem setValue:@"lion king" forKey:@"title"];
...
} 

-(IBAction) updateCastBtn:(UIButton *)sender{
...
[movieItem setValue:@"forest whitaker" forKey:@"cast"];
...
} 

Upvotes: 0

Viper
Viper

Reputation: 1408

movieItem is local variable and will be accessible only in viewDidLoad method's scope. to access it in another function please make it global or use property.

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

You get that error because movieItem is a local variable that's only valid within the viewDidLoad method where you created it. You need to create an ivar or property.

Upvotes: 2

Related Questions