Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-04-2011, 08:58 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Unhappy iPhone delegation issue

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.
JheeBz is offline   Reply With Quote
Old 10-06-2011, 12:31 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

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.
JheeBz is offline   Reply With Quote
Old 10-06-2011, 01:11 AM   #3 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

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 thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 10-06-2011, 03:54 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
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.
JheeBz is offline   Reply With Quote
Old 10-06-2011, 09:18 AM   #5 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

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 thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 10-07-2011, 09:40 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 10
JheeBz is on a distinguished road
Default

Thank you very much This has proven to be much easier than attempting to implement delegation.
JheeBz is offline   Reply With Quote
Reply

Bookmarks

Tags
array, delegate, delegation, memory-management, protocol

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 401
17 members and 384 guests
13dario13, 7twenty7, eski, EvilElf, glenn_sayers, HemiMG, iOS.Lover, jarv, LunarMoon, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:21 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0