我尝试实现User define 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)为什么这不起作用,我做错了什么? 谢谢你的重播
答案 0 :(得分: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;
}