iOS Message Sending btw two objects

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
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 ?
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
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.
 

dejo

Moderator
Staff member
Sep 2, 2004
15,981
447
The Centennial State
What is the value of redLightDel when this method runs?:
Code:
- (void) redLightActivated
{
    [self.redLightDel redLightSwitchedOn];
}
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
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 ?
 

dejo

Moderator
Staff member
Sep 2, 2004
15,981
447
The Centennial State
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 ?
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?