簡體   English   中英

為什么我的自定義iPhone代表無法正常工作?

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

我只是試圖構建一個簡單的委托,它可以成功編譯,但是現在似乎什么也沒做。 請在下面查看我的代碼,FirstViewContoller和SecondViewController都有單獨的xib文件,SecondViewController的按鈕當然與myMethod掛鈎。

我學習目標C和iPhone SDK的方法已經有幾個月了,我一直在使用一本書和lynda.com教程來提供幫助,實際上我使用實用程序應用程序的xcode模板復制了大部分代碼,而且我相信我了解這里發生的事情,但是由於按鈕沒有響應,我錯過了一些事情。

如果您需要更多信息,請詢問。

非常感謝克里斯

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

@implementation SecondViewController

@synthesize delegate;

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

FirstViewController.h

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

FirstViewController.m

@implementation FirstViewController

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

編輯 -這是一篇舊文章,但為了澄清起見,我只是忘了在FirstViewController中分配SecondViewController委托

如何分配SecondViewController的委托屬性? 這是我最想念的部分...

兩件事情:

  1. 您的IBAction方法myMethod如果確實是IBAction,則需要接受一個參數-(IBAction)myMethod:(id)sender在怪罪委托模式之前,請確保已調用myMethod(設置斷點並查看)。

  2. 您的委托方法似乎已聲明采用委托類型作為參數,而不是您似乎希望傳遞給那里的viewController。 可能會使所有的id類型都模糊不清,但是我認為您可能想重新定義聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM