Issue with Custom Protocol Delegate

Discussion in 'iOS Programming' started by donaghy, Feb 7, 2010.

  1. donaghy macrumors member

    Joined:
    Aug 6, 2009
    #1
    I have created a UIViewController class with a custom protocol delegate. The view controller has nib with a picker control and button. The issue occurs when I click the select button, it goes into (IBAction) btnSelect method and then in this method should execute the delegate Test method but it never does, it always says delegate is nil. Here is the code:

    Code:
    
    #import <UIKit/UIKit.h>
    
    @protocol PickerViewDelegate <NSObject>
    @required
    - (void) test: (NSString *)value;
    
    @end
    
    @interface PickerView : UIViewController
    {
    	IBOutlet UIDatePicker *dtePicker;
    	id<PickerViewDelegate> delegate;
    }
    
    @property (nonatomic, retain) IBOutlet UIDatePicker *dtePicker;
    @property (nonatomic, retain) id<PickerViewDelegate> delegate; 
    
    - (IBAction) btnSelect;
    - (void) PickerShow;
    
    + (void) PickerShow;
    + (id) sharedInstance;
    
    @end
    
    
    #import "PickerView.h"
    
    static PickerView *sharedView = nil;
    
    @implementation PickerView
    
    @synthesize dtePicker;
    @synthesize delegate;
    
    - (void)dealloc 
    {
        [super dealloc];
    }
    
    
    + (void) PickerShow
    {
    	if (!sharedView) 
    	{
    		sharedView = [[[PickerView alloc] init] autorelease];
    	}
    	[sharedView PickerShow];
    }
    
    
    - (void) PickerShow
    {	
    	UIWindow* window = [UIApplication sharedApplication].keyWindow;
    	if (!window) {
    		window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    	}
    	
    	[UIView beginAnimations:nil context:nil];
    	[UIView setAnimationDuration:0.5];
    	
    	CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 250);
    	self.view.transform = transform;
    	[window addSubview: self.view];
    	[UIView commitAnimations];	
    }
    
    - (IBAction) btnSelect
    {
    	if ([self.delegate respondsToSelector:@selector(test)]) {
    	[delegate test:@"test"];
    	}
    	value = [dtePicker date]; 
    }
    
    
    + (id) sharedInstance {
    	if (!sharedView) 
    	{
    		sharedView = [[[PickerView alloc] init] autorelease];
    	}
    
    	return sharedView;
    }
    
    @end
    
    
    I can implement using the following:

    Code:
    @implementation SecondLevel
    
    - (void) viewDidLoad 
    {
        [super viewDidLoad];
    	
    	[[PickerView sharedInstance] retain];
    	[PickerView PickerShow];
    }
    
    - (void) test: (NSString *) value
    {
    	NSLog(@"test");
    }
    
    
    @end
    
    The above shows the View with the picker control and Select button. The issue is when i click the select button, it goes into (void)btnSelect but the delegate is nil and it never executes the method. Can someone point me in right direction?

    Thanks.
     
  2. donaghy thread starter macrumors member

    Joined:
    Aug 6, 2009
    #2
    Ok think i've found a solution to my own problem. Just needed to set the delegate in the calling class like so:

    Code:
    newPickerView = [[[PickerView alloc] init] retain];
    	[newPickerView setDelegate:self];
    	[newPickerView PickerShow];
    
    I'm still not confident this is the best approach. Any guidance is appreciated, thanks.
     

Share This Page