现在的模态视图控制器

时间:2010-07-21 23:55:31

标签: iphone objective-c cocoa-touch

我刚刚开始使用iphone开发 我有一个Tabbed应用程序,我想以模态方式显示一个登录表单 所以我看了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];
}

当我建立时,我得到“成员'代表'的请求,而不是结构或联合” 如果我摆脱第二行,它会构建,但按下按钮不会做任何事情。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:20)

听起来您还没有为LoginForm声明delegate成员。您需要添加代码,以便在LoginForm完成时让UIViewController实例以模态方式呈现LoginForm。以下是如何声明自己的委托:

在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):

在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];
}

答案 1 :(得分:0)

您的LoginForm类似乎来自UIViewControllerUIViewController类没有delegate属性,因此您得到了编译错误。

您的问题可能是首先没有调用该操作。行动的正确签名是:

- (IBAction)showLogin:(id)sender;

sender参数是必需的。在方法中放置一个断点以确保它被调用。