开发者

Simple delegate method does not work

开发者 https://www.devze.com 2023-03-15 16:05 出处:网络
Some help would be appreciated. I have this simple project for testing purposes: http://dl.dropbox.com/u/10101053/testDelegate.zip

Some help would be appreciated.

I have this simple project for testing purposes:

http://dl.dropbox.com/u/10101053/testDelegate.zip

I would like to pass a NSString in a delegate method but with this code does not work.

testDelegateViewController.h

@protocol testDelegateViewControllerDelegate; 


@interface testDelegateViewController : UIViewController {


id<testDelegateViewControllerDelegate> delegate;

IBOutlet UIButton *button;

}

@property (nonatomic, assign) id<testDelegateViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIButton *button;

- (void)pass;

@end

@protocol testDelegateViewControllerDelegate

- (void)passSomeToDelegate:(NSString *)some;

@end

testDelegateVewController.m

#import "testDelegateViewController.h"

@implementation testDelegateViewController
@synthesize delegate, button;


- (void)pass
{
    NSLog(@"Button Pressed");
    [self.delegate passSomeToDelegate:@"some"];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];


    [button addTarget:self action:@selector开发者_如何学运维(pass) forControlEvents:UIControlEventTouchUpInside];
}

//rest of code

AppDelegate.h

#import "testDelegateViewController.h"

@interface AppDelegate : NSObject <UIApplicationDelegate, testDelegateViewControllerDelegate> {

}

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate


@synthesize window=_window;

@synthesize viewController=_viewController;

#pragma mark Delegate Method

- (void)passSomeToDelegate:(NSString *)some
{
    NSLog(@"%@", some);
}

//rest of code

But in my console nothing is printed when button is tapped.

Thanks


You forgot to set your delegate. You can set the delegate in the application:didFinishLaunchingWithOptions: method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    self.viewController.delegate = self;

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

I added self.viewController.delegate = self; to set the delegate.

0

精彩评论

暂无评论...
验证码 换一张
取 消