Search code examples
objective-cxcodexcode4delegatesobjective-c-category

User Define Delegate Not working?


I try to implement User define Delegate.For that i did this code

**- DelegateAppDelegate.h**
#import <UIKit/UIKit.h>
#import "viewApp.h"
@interface DelegateAppDelegate : NSObject <UIApplicationDelegate,SampleDeledate> 
{

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

 **- DelegateAppDelegate.h**

#import "DelegateAppDelegate.h"

@implementation DelegateAppDelegate


@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    viewApp *v=[[view alloc]initWithNibName:@"viewApp" bundle:nil];
    [_window addSubview:v.view];   
    return YES;
}


- (void)dealloc
{
    [_window release];
    [super dealloc];
}
@end

@implementation DelegateAppDelegate(SampleDeledate)

-(void)AppImageDidLoad:(NSString*)Name;
{
    NSLog(@"hi........%@",Name);
}
@end
 **- viewApp.h**
#import <UIKit/UIKit.h>
@class viewApp;


@protocol SampleDeledate;

@protocol SampleDeledate <NSObject>
    @required
        -(void)AppImageDidLoad:(NSString*)Name;
@end

@interface viewApp : UIViewController 
{
    id<SampleDeledate>delegate;
}

@property(nonatomic,assign)id<SampleDeledate>delegate;

-(IBAction)DelegateFunction:(id)sender;
@end

 **- viewApp.m**

#import "viewApp.h"

@implementation view
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {  }
    return self;
}
- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
      [super didReceiveMemoryWarning];    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}
-(IBAction)DelegateFunction:(id)sender
{


  [self.delegate AppImageDidLoad:@"Delegate sample"];

}
@end

In this coding my intention is When i click button, Corresponding Action get executed( -(IBAction)DelegateFunction:(id)sender),then i want to call the AppImageDidLoad delegate method, But in my coding this is not working, 1)Why this is not working,Any wrong i did? thanks for your replay


Solution

  • In the following method

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
        viewApp *v=[[view alloc]initWithNibName:@"viewApp" bundle:nil];
        [_window addSubview:v.view];   
        return YES;
    }
    

    add one more line and make it as follows.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        viewApp *v=[[view alloc]initWithNibName:@"viewApp" bundle:nil];
        v.delegate = self;
        [_window addSubview:v.view];   
        return YES;
    }