Hello there,
I've been having an issue with my program for the past week and a half and I simply cannot figure out what's wrong with my program. Basically there are 2 views that I'm trying to work with "FirstView" and "SecondView". I am trying to get data from the SecondView to the FirstView through a method defined in a protocol.
FirstViewController.h
Code:
#import <UIKit/UIKit.h>
#import "Drink.h"
@protocol firstViewControllerDelegate <NSObject>
- (NSMutableArray *) getFavourites;
- (void) setFavourites: NSMutableArray;
@end
@interface FirstViewController : UIViewController
{
id <firstViewControllerDelegate> delegate;
NSMutableArray *labels;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *button4;
}
- (IBAction) buttonClick: (id) sender;
@property (nonatomic, assign) id <firstViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;
@property (nonatomic, retain) IBOutlet UIButton *button4;
@end
This is where the issue is occuring. I am trying to call the getFavourites method defined in the protocol, which returns nothing. HOWEVER, when I was in the secondView, I used the debugger to check the value of "favourites" and it actually had values in it (they were out of scope though). As soon as I changed the view to the FirstView, the favourites array was empty again.
FirstViewController.m
Code:
#import "FirstViewController.h"
#import "Drink.h"
@implementation FirstViewController
@synthesize button1, button2, button3, button4;
@synthesize delegate;
.........
- (void) viewDidAppear:(BOOL)animated
{
NSMutableArray *favourites = [self.delegate getFavourites];
// Favourites is empty
Drink *drink = [favourites objectAtIndex:1];
NSLog(@"NAME VAR IS: %@", [drink name]);
[button1 setTitle:[[favourites objectAtIndex:1] name] forState:UIControlStateNormal];
NSLog(@"VIEW APPEARED. BUTTON TITLE IS: %@", button1.currentTitle);
}
.......
SecondViewController.h
Code:
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface SecondViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, firstViewControllerDelegate>
{
UITableView *PrefsTable;
NSMutableArray *favourites;
NSMutableArray *drinks;
}
@property (nonatomic, retain) IBOutlet UITableView *PrefsTable;
@property (nonatomic, retain) NSMutableArray *drinks;
@property (nonatomic, retain, getter = getFavourites) NSMutableArray *favourites;
- (void) addDrink: (NSString *) name;
@end
So here in the SecondViewController.m, I call the getFavourites method defined in the property (that I then use through a protocol in FirstView), which actually works here.
SecondViewController.m
Code:
#import "SecondViewController.h"
#import "Drink.h"
@implementation SecondViewController
@synthesize PrefsTable;
@synthesize drinks, favourites;
........
- (void)viewDidDisappear:(BOOL)animated
{
NSMutableArray *array = [self getFavourites]; // THIS METHOD WORKS IN THIS CLASS
}
I really do not know what is causing this issue, I theorise that perhaps the array is released or something when switching between the views, but I really don't know. Any help would be much appreciated as I have been battling this issue for the past week and a half.