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:
I can implement using the following:
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.
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.