2011-11-22 76 views
0

我尝试实现用户自定义Delegate.For,我没有这个代码用户定义委托不工作?

**- 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 

在这个编码我的本意是,当我点击按钮,相应的动作得到执行( - (IBAction为)DelegateFunction:(ID)发送方) ,那么我想调用AppImageDidLoad委托方法,但在我的代码中这是行不通的, 1)为什么这不起作用,我做错了? 感谢您的重播

回答

1

在下面的方法

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

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

增加一个行,使其如下。

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