简体   繁体   English

为什么我的自定义iPhone代表无法正常工作?

[英]Why isn't my custom iPhone delegate working?

I'm just trying to build a simple delegate, it compiles successfully but it doesn't seem to do anything right now. 我只是试图构建一个简单的委托,它可以成功编译,但是现在似乎什么也没做。 Please review my code below, both FirstViewContoller and SecondViewController have separate xib files, the SecondViewController has a button hooked up to the myMethod of course. 请在下面查看我的代码,FirstViewContoller和SecondViewController都有单独的xib文件,SecondViewController的按钮当然与myMethod挂钩。

I've been learning my way around objective c and iPhone SDK for a couple of months, I've been using a book and lynda.com tutorial to aid, I actually copied most of the code using the xcode template for a utility app, and I believe I understand what is going on here, but is there something I've missed, as the button isn't responding. 我学习目标C和iPhone SDK的方法已经有几个月了,我一直在使用一本书和lynda.com教程来提供帮助,实际上我使用实用程序应用程序的xcode模板复制了大部分代码,而且我相信我了解这里发生的事情,但是由于按钮没有响应,我错过了一些事情。

If you need more info please ask. 如果您需要更多信息,请询问。

Many thanks, Chris 非常感谢克里斯

SecondViewController.h SecondViewController.h

#import <UIKit/UIKit.h>

@protocol SecondViewControllerDelegate;


@interface SecondViewController : UIViewController {
    id <SecondViewControllerDelegate> delegate;
}

@property (nonatomic, assign) id <SecondViewControllerDelegate> delegate;


-(IBAction) myMethod;

@end

@protocol SecondViewControllerDelegate

-(void)viewControllerDidFinish:(SecondViewController *)controller;

@end

SecondViewController.m SecondViewController.m

@implementation SecondViewController

@synthesize delegate;

-(IBAction) myMethod
{
    [self.delegate viewControllerDidFinish:self];
}
//
@end

FirstViewController.h FirstViewController.h

@interface FirstViewController : UIViewController <SecondViewControllerDelegate>{
//
@end

FirstViewController.m FirstViewController.m

@implementation FirstViewController

-(void)viewControllerDidFinish:(SecondViewControllerDelegate *)controller
{
    NSLog(@"delegate is being used");
}
//
@end

Edit - This is an old post but just to clarify I simply forgot to assign the SecondViewController delegate in the FirstViewController 编辑 -这是一篇旧文章,但为了澄清起见,我只是忘了在FirstViewController中分配SecondViewController委托

How do you assign the delegate property of SecondViewController? 如何分配SecondViewController的委托属性? This is the last part I am missing... 这是我最想念的部分...

Two things: 两件事情:

  1. Your IBAction method myMethod , if it really is an IBAction, needs to take an argument: -(IBAction)myMethod:(id)sender Before you blame the delegate pattern, make sure that myMethod is being called (set a breakpoint and see). 您的IBAction方法myMethod如果确实是IBAction,则需要接受一个参数-(IBAction)myMethod:(id)sender在怪罪委托模式之前,请确保已调用myMethod(设置断点并查看)。

  2. Your delegate method seems to be declared to take a delegate type as a param, not a viewController which is what you seem to want to be passing there. 您的委托方法似乎已声明采用委托类型作为参数,而不是您似乎希望传递给那里的viewController。 That might all get obscured by the id types, but I think you probably want to rejigger the declarations. 可能会使所有的id类型都模糊不清,但是我认为您可能想重新定义声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM