Пользователь Определить Делегат Не работает? - PullRequest
0 голосов
/ 22 ноября 2011

Я пытаюсь реализовать определение User Delegate. Для этого я сделал этот код

**- 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) sender), затем я хочу вызвать метод делегата AppImageDidLoad, но в моем коде это не работает, 1) Почему это не работает, я не так сделал? спасибо за ваш повтор

1 Ответ

1 голос
/ 22 ноября 2011

В следующем методе

- (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;
}
...