目前模态视图控制器

问题描述:

我刚开始使用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

听起来像您还没有为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

看起来你的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