[英]present modal view controller
我剛剛開始進行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];
}
當我構建時,我得到“在不是結構或聯合的東西中請求成員'委托'”。如果我擺脫了第二行,則它會構建,但是按下按鈕不會執行任何操作。
我在這里想念什么?
聽起來您尚未聲明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):
在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];
}
看來您的LoginForm
類是從UIViewController
派生的。 UIViewController
類沒有delegate
屬性,因此出現編譯錯誤。
您的問題可能是該動作最初沒有被調用。 動作的正確簽名是:
- (IBAction)showLogin:(id)sender;
sender
參數是必需的。 在您的方法中放置一個斷點以確保它被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.