2012-05-06 13 views
0

我有一个自定义UIViewController显示与presentModalViewController函数在http基本认证委托获取用户名和密码。我想等到用户点击屏幕上显示的模式视图控制器上的登录按钮。我怎样才能做到这一点?我是iOS新手,任何意见或链接将不胜感激。如何等待,直到模式对话框返回结果在ios

编辑:这里是内NSURLConnectionDelegate

-(void) connection(NSURLConnection*)connection willSendRequestForAuthenticationChallenge(NSURLAuthenticationChallenge*)challenge 
{ 
    CustomAuthViewController *authView = [CustomAuthViewController alloc] initWithNibName"@"CustomAuthViewController" bundle:[NSBundle mainBundle]]; 
    [parentcontroller presentModalViewController:authView animated:YES]; 
    // 
    // I want to wait here somehow till the user enters the username/password 
    // 
    [[challenge sender] userCredentials:credentials forAuthenticationChallenge:challenge]; 
} 

亲切的问候一个示例代码。

编辑:解决方案:不必立即发送willSendRequestForAuthenticationChallenge委托函数中的凭据。我可以随时发送它,但是很奇怪。

+0

一旦用户点击按钮,你可以关闭modalViewController,是否你想要什么? – Peres

+0

不幸的是没有!我正在显示一个自定义的UIViewController来收集用户/密码在willSendRequestForAuthenticationChallenge函数中。现在,我必须等到用户输入用户名/密码并单击登录按钮视图...否则我将无法在presentModalViewController后的下一行发送userCredentials:forAuthenticationChallenge :( – CodeWeed

回答

6

基本上你想要的是当你的登录对话框完成时,将消息从模态UIViewController传递给调用者。有很多方法可以做到这一点。这里有一对夫妇:

选项1 - 代理模式:

在您的模式对话框的.h

@protocol LoginDelegate 
- (void)loginComplete:(NSString *)userId; 
- (void)loginFailed; 
@end 

@interface MyLoginDialog : UIViewController { 
    UIViewController *delegate; 
} 

@property (nonatomic, retain) UIViewController *delegate; 

在您的模式对话框的.m

在你的init

delegate = nil; 

您的dealloc中:

[delegate release]; 

当您完成登录:

[delegate dismissModalViewControllerAnimated:YES]; 
[delegate loginComplete:userId] or [delegate loginFailed]; 

然后您的电话视图控制器上实现LoginDelegate协议。

当你创建你的登录视图控制器,设置委托:

UIViewController *viewLogin = [[UIViewController alloc] init]; 
viewLogin.delegate = self; 

选择2 - 邮政与NSNotificationCenter通知:

上的登录对话框:

[self dismissModalViewControllerAnimated:YES]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"LoginComplete" object:nil]; 

在您的来电视图控制器

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginComplete:) name:@"LoginComplete" object:nil]; 

然后你实现了选择器loginComplete。

如果您想传回登录信息(username,userId等),可以将其打包到字典中,并将其作为“对象”添加到postNotificationName方法中。

你还需要确保调用

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

你做听力的时候。

+0

没问题。请致电/接受,如果答案是有帮助/为你工作 – Joel

+0

嗨Joel,感谢您的努力,我真的很感激。willSendRequestForAuthenticationChallenge功能是要求userCredentials,所以在我用presentModalViewController函数显示对话框后,我将如何等待? – CodeWeed

+0

您不需要在代码块中“等待”异步操作,而是在完成登录对话框时,在回调方法中实现了willSendRequestForAuthenticationChallenge。 – Joel

相关问题