Message Sending btw two objects

Discussion in 'iOS Programming' started by erdinc27, Jun 20, 2013.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #1
    hey guys. i want to develop a project to learn something new. so i created a scenario. i have Two classes(TheDriver and TrafficLights). here the classes.

    TheDriver.h
    Code:
    #import <Foundation/Foundation.h>
    
    
    @interface TheDriver : NSObject
    
    @property (retain, nonatomic) NSString *driverName;
    @property (assign, nonatomic) NSInteger drvierAge;
    @property (assign, nonatomic) NSInteger driverLicenseCode;
    
    TheDriver.m
    Code:
    #import "TheDriver.h"
    
    @implementation TheDriver
    
    @synthesize driverName;
    @synthesize driverLicenseCode;
    @synthesize drvierAge;
    
    
    - (id) init
    {
        self = [super init];
        
        if (self)
        {
            
        }
        
        return self;
    }
    
    TrafficLights.h
    Code:
    #import <Foundation/Foundation.h>
    
    
    @interface TrafficLights : NSObject
    
    - (void) redLineActived;
    - (void) greenLineActivated;
    
    
    TrafficLights.m
    Code:
    
    #import "TrafficLights.h"
    
    @implementation TrafficLights
    
    
    - (void) redLineActived
    {
        
    }
    
    
    - (void) greenLineActivated
    {
        
    }
    
    @end
    
    i want the driver will push gas pedal when the traffic light is green and if the red light is activated the driver will push break pedal.
    i try to make it with delegate but i couldn't achieve. anyone who can help me about this ?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You should show us the code where you tried to use delegation. We can't guess what it was. Also, what do you mean by you "couldn't achieve"? Please elaborate.
     
  3. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #3
    i tried something like this

    TheDriver.h
    Code:
    #import <Foundation/Foundation.h>
    #import "TrafficLights.h"
    
    
    @interface TheDriver : NSObject<redLightDelegate>
    
    @property (retain, nonatomic) NSString *driverName;
    @property (assign, nonatomic) NSInteger drvierAge;
    @property (assign, nonatomic) NSInteger driverLicenseCode;
    
    TheDriver.m
    Code:
    #import "TheDriver.h"
    
    @implementation TheDriver
    
    @synthesize driverName;
    @synthesize driverLicenseCode;
    @synthesize drvierAge;
    
    
    - (id) init
    {
        self = [super init];
        
        if (self)
        {
            
        }
        
        return self;
    }
    
    
    - (void) redLightSwitchedOn
    {
        NSLog(@"The Driver Should Push Break Pedal");
    }
    
    TrafficLights.h
    Code:
    #import <Foundation/Foundation.h>
    
    //@class TrafficLights;
    
    @protocol redLightDelegate <NSObject>
    - (void) redLightSwitchedOn;
    @end
    
    @interface TrafficLights : NSObject
    
    @property (nonatomic, strong) id <redLightDelegate> redLightDel;
    
    - (void) redLightActivated;
    - (void) greenLineActivated;
    
    TrafficLights.m
    Code:
    #import "TrafficLights.h"
    
    @implementation TrafficLights
    @synthesize redLightDel;
    
    - (void) redLightActivated
    {
        [self.redLightDel redLightSwitchedOn];
    }
    
    
    - (void) greenLineActivated
    {
        
    }
    
    and in viewDidLoad method of viewController i wrote like this
    Code:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        
        TrafficLights *myTrafficLight=[[TrafficLights alloc] init];
        [myTrafficLight redLightActivated];
    
    }
    
    but in Log screen nothing is seen.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    What is the value of redLightDel when this method runs?:
    Code:
    - (void) redLightActivated
    {
        [self.redLightDel redLightSwitchedOn];
    }
     
  5. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #5
    when this method runs i expect to be triggered "redLightSwitchedOn" method in "TheDriver.m" filen and written in Log screen but nothing is written there. i cant find where i make the mistake.
    should i assign the delegate somewhere to any object ?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    That doesn't really answer my question. Do some basic debugging. When that line of code runs, what is the value of redLightDel? Is it nil or does it point to some redLightDelegate instance? If it's nil, is that a problem?
     

Share This Page