2010-07-21 124 views
11

我剛開始使用iPhone開發 我有一個標籤式應用程序,我想顯示日誌形式模態 ,所以我看了這裏Apple Dev,這樣做,在我的視圖控制器 之一,我連一個按鈕下面的操作:目前模態視圖控制器

#import "LoginForm.h" 
... 
-(IBAction)showLogin{ 
LoginForm *lf = [[LoginForm alloc]initWithNibName:@"LoginForm" bundle:nil]; 
lf.delegate = self; 
lf.modalPresentationStyle = UIModalTransitionStyleCrossDissolve; 
[self presentModalViewController:lf animated:YES]; 
} 

,當我建我得到「會員‘代理’要求的東西不是一個結構或聯合」 如果我擺脫二線的,它建立但按下該按鈕並沒有。

我在這裏錯過了什麼?

+0

如果我在ViewBased應用使用相同的代碼我得到在第二行相同的錯誤,但如果我刪除線時我按下按鈕出現的模態圖。 ..我爲代表團需要一些特別的東西嗎?和標籤模板? – irco 2010-07-21 23:56:35

回答

19

聽起來像您還沒有爲LoginForm聲明delegate成員。您需要添加代碼,以便在LoginForm完成時以模態方式呈現LoginForm的UIViewController實例。以下是聲明自己的委託:

在LoginForm.h:

@class LoginForm; 

@protocol LoginFormDelegate 
- (void)loginFormDidFinish:(LoginForm*)loginForm; 
@end 

@interface LoginForm { 
    // ... all your other members ... 
    id<LoginFormDelegate> delegate; 
} 

// ... all your other methods and properties ... 

@property (retain) id<LoginFormDelegate> delegate; 

@end 

在LoginForm.m:

@implementation 

@synthesize delegate; 

//... the rest of LoginForm's implementation ... 

@end 

然後在呈現LoginForm的該UIViewController的實例(姑且稱之爲MyViewController) :

In MyViewController.h:

@interface MyViewController : UIViewController <LoginFormDelegate> 

@end 

在MyViewController.m:

/** 
* LoginFormDelegate implementation 
*/ 
- (void)loginFormDidFinish:(LoginForm*)loginForm { 
    // do whatever, then 
    // hide the modal view 
    [self dismissModalViewControllerAnimated:YES]; 
    // clean up 
    [loginForm release]; 
} 

- (IBAction)showLogin:(id)sender { 
    LoginForm *lf = [[LoginForm alloc]initWithNibName:@"LoginForm" bundle:nil]; 
    lf.delegate = self; 
    lf.modalPresentationStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:lf animated:YES]; 
} 
+0

非常感謝...這就是我一直在尋找的東西。 它在協議聲明中說的最後一件事是,我不知道: 「預期」)'之前LoginForm「 我沒有看到它有太多的錯誤。與你的代碼唯一的區別是,我的表單是從UIViewController繼承,但它看起來不像是與該錯誤相關 – irco 2010-07-22 01:05:30

+0

我的壞...我在協議聲明之前忘了'@class LoginForm;'。我在我的答案中編輯了源代碼。 – 2010-07-22 01:13:05

+0

感謝,我也的確在MyViewController的進口,以便它可以看到的協議,它編譯,但它擊中ShowLogin函數的第一行 控制檯顯示未捕獲的異常 「NSInvalidArgumentException」的前仍然崩潰,原因:' - [UIViewController showLogin]:無法識別的選擇發送到實例0x5936080' – irco 2010-07-22 01:34:44

0

看起來你的LoginForm類來自UIViewControllerUIViewController類沒有delegate屬性,因此存在編譯錯誤。

您的問題可能是該行爲不首先被調用。一個動作的正確簽名是:

- (IBAction)showLogin:(id)sender; 

sender參數是必需的。在你的方法中加入一個斷點來確保它被調用。

+0

我該如何聲明loginForm的委託? 和是的,我認爲你是對的,我沒有看到被擊中的斷點 – irco 2010-07-22 00:27:40

+0

這是不正確的。一個動作方法可以接受零參數或一個(控制器發送它),並且Interface Builder將很樂意將控件掛接到 - (IBAction)。 – 2010-07-22 00:44:21

相關問題