我已经创建了一个循环,在代码移动之前等待用户在警报中填写框时运行。我在其他地方使用相同的循环,它完美无缺。
然而,在这里,它只在第一次被调用时循环,随后创建另一个警报并再次被调用。第二次运行,直到用户完成输入详细信息为止。
//Method waiting for users credentials
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"got auth challange");
_didChallenge = YES;
// Ask user for their credentials
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Please enter username and password:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alertView setTag:1];
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView show];
[self performSelectorOnMainThread:@selector(WaitForCredentialDialog) withObject:nil waitUntilDone:YES];
//... Code to deal with credentials is here.
}
//Here dialogResult is a variable which will make while loop run until its value is -1 and reset its value to 1 or 0 when AlertView's Button is clicked
- (void) WaitForCredentialDialog{
NSDate* LoopUntil;
LoopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];
while ((dialogResult==-1) && ([[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:LoopUntil]))
{
LoopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];
}
}
答案 0 :(得分:2)
你不应该使用循环!使用UITextFieldDelegate
获取有关用户输入内容的事件。
要检测文本字段中的更改,请使用:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
或UIAlertViewDelegate
' s:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
在首次显示警报视图时以及用户每次将字符键入其中一个文本字段时都会调用此方法,这使得在接受用户值之前很容易执行基本输入验证
来源: