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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-26-2011, 03:00 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 50
schenker is on a distinguished road
Default delegate not working

Hello, i have problem calling a method using delegate. Pls take a look at my simplified code and tell me what's wrong.

Protocol declaration file:
Code:
//  UpdateCurrentSumDelegate.h

#import <Foundation/Foundation.h>

@protocol UpdateCurrentSumDelegate <NSObject>

@required
-(void)UpdateCurrentSum;
@end

Header file of class that implements the protocol:
Code:
//  Cube1ViewController.h

#import "UpdateCurrentSumDelegate.h"

@interface Cube1ViewController : UIViewController <UpdateCurrentSumDelegate>{
    
    id <UpdateCurrentSumDelegate> delegate;
    
    Cube1ViewController *cube1vc;		
}

@property (nonatomic, assign) id delegate;

@end
Implementation file of class that implements the protocol:
Code:
//  Cube1ViewController.m

#import "Cube1ViewController.h"

@implementation Cube1ViewController

@synthesize delegate;

-(Cube1ViewController *)cube1vc {
    if (!cube1vc) 
    {
      cube1vc=[[Cube1ViewController alloc]init]; 
    }
    return cube1vc;
}


- (void)viewDidLoad {

     self.cube1vc.delegate = self;
    [self.delegate UpdateCurrentSum];
   }


-(void)UpdateCurrentSum {
    NSLog(@"this doesnt gets called by delegate!!!"); 
}

So, what is wrong? Pls help me. Thanks.
schenker is offline   Reply With Quote
Old 08-04-2011, 07:38 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: Edinburgh
Posts: 209
Justinmichael is on a distinguished road
Default

From a quick glance at the code, you are setting the delegate on the var cubelvc and expecting it to call back in self. This almost would work but ultimately it is flawed. You've got a reference minefield going on here as you are creating a object with a reference to another of object of the same type and the order of viewDidLoad is throwing you so that the cube1vc object doesn't call the delegate after it has received it's delegate setup.

An overview of a simple delegate pattern:

ClassA.h

Code:
ClassA : NSObject <Delegate> {

    ClassB* b;

}
-----------
ClassA.m
Code:
- (id)init {
   ....
   b = [[ClassB alloc] init];
   b.delegate = self;[
  [b doSomething];
   ...
}

- (void)delegateCallBack {
   NSLog(@"here");
}
ClassB.h
Code:
id <Delegate> delegate;

@property .... delegate;
ClassB.m
Code:
@synthesize delegate;

-(void)doSomething {
    [delegate delegateCallBack];
}
__________________

Last edited by Justinmichael; 08-04-2011 at 07:44 AM.
Justinmichael is offline   Reply With Quote
Reply

Bookmarks

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: 411
11 members and 400 guests
AppleDev, chemistry, Emy, Gi-lo, hussain1982, ipodphone, mistergreen2011, pipposanta, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,923
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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