Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

iPhone delegation issue

JheeBzJheeBz Posts: 10Registered Users
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

#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

#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


#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


#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.
Post edited by JheeBz on

Replies

  • JheeBzJheeBz Posts: 10Registered Users
    I can't tell whether the delegate actually has a value or not because the debugger does not show anything when I hover over the "delegate" property. I read somewhere that you should "pass" the delegate but I don't know know what this means. I would really appreciate some help on this as this is a majorly blocking issue.
  • DomeleDomele Posts: 2,948Registered Users
    Not how you should use delegates but for the purpose of learning how to set one up, I'll guide you through this. Also, if you do make delegate methods, don't implement them using a property. How are you pushing first view from second view? Are you setting the delegate property?
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • JheeBzJheeBz Posts: 10Registered Users
    Domele;379713 said:
    Not how you should use delegates but for the purpose of learning how to set one up, I'll guide you through this. Also, if you do make delegate methods, don't implement them using a property. How are you pushing first view from second view? Are you setting the delegate property?
    I've created a delegate method before and it worked in my other program. Also, I forgot to mention that I switch between the different views through tabs as this is a tab based app. I've created the delegate property as a member variable of FirstViewController as you can see and made it a property. I'm not entirely sure what you mean by that.
  • DomeleDomele Posts: 2,948Registered Users
    Okay then, if you already know how to, do this the right way. Use a Singleton data object to share data. Duncan C uses one in his generate random text tutorials and there a bunch of tutorials online. Delegation is not the correct choice to share data like this.
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • JheeBzJheeBz Posts: 10Registered Users
    Thank you very much :) This has proven to be much easier than attempting to implement delegation.
Sign In or Register to comment.