如何等到模态对话框在ios中返回结果

Cod*_*eed 1 objective-c ios

我有一个自定义UIViewController与显示presentModalViewController在HTTP基本认证委托函数获取用户名和密码.我想等到用户点击屏幕上显示的模态视图控制器上的登录按钮.我怎样才能做到这一点?我是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];
}
Run Code Online (Sandbox Code Playgroud)

亲切的问候.

编辑:解决方案:没有必要立即在willSendRequestForAuthenticationChallenge委托函数中发送凭据.我可以在以后随时发送,但这很奇怪.

Joe*_*oel 6

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

选项1 - 代表模式:

在你的模态对话框上.h

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

@interface MyLoginDialog : UIViewController {
    UIViewController *delegate;
}

@property (nonatomic, retain) UIViewController *delegate;
Run Code Online (Sandbox Code Playgroud)

在你的模态对话框.m

在你的init中:

delegate = nil;
Run Code Online (Sandbox Code Playgroud)

在你的dealloc:

[delegate release];
Run Code Online (Sandbox Code Playgroud)

当您完成登录时:

[delegate dismissModalViewControllerAnimated:YES]; 
[delegate loginComplete:userId] or [delegate loginFailed];
Run Code Online (Sandbox Code Playgroud)

然后在您的调用视图控制器上实现LoginDelegate协议.

在创建登录视图控制器时,设置委托:

UIViewController *viewLogin = [[UIViewController alloc] init];
viewLogin.delegate = self;
Run Code Online (Sandbox Code Playgroud)

选项2 - 向NSNotificationCenter发布通知:

在您的登录对话框中:

[self dismissModalViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LoginComplete" object:nil];
Run Code Online (Sandbox Code Playgroud)

在您的呼叫视图控制器上

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginComplete:) name:@"LoginComplete" object:nil];
Run Code Online (Sandbox Code Playgroud)

然后实现选择器loginComplete.

如果要传回登录信息(用户名,userId等),可以将其打包到字典中,并将其添加为postNotificationName方法中的"对象".

你还需要确保打电话

[[NSNotificationCenter defaultCenter] removeObserver:self];  
Run Code Online (Sandbox Code Playgroud)

当你完成听力.