2011-11-22 74 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; 
}