问题
I have an alertview that is being created with a textfield inside of it. when i close the alertview via a submit or cancel button, i am getting the wait_fences error in the console. it doesnt crash, or i havent been able to make it crash but id really like to figure out what is going on.
alert = [[UIAlertView alloc]
initWithTitle:@"Lookup"
message:@"\n\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Submit", nil];
label = [[UILabel alloc] initWithFrame:CGRectMake(12, 40, 260, 25)];
label.font = [UIFont systemFontOfSize:16];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0, -1);
label.textAlignment = UITextAlignmentCenter;
label.text = @"Enter 10-Digit ISBN Number";
[alert addSubview:label];
field = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
field.font = [UIFont systemFontOfSize:18];
field.backgroundColor = [UIColor whiteColor];
field.keyboardAppearance = UIKeyboardAppearanceAlert;
field.keyboardType = UIKeyboardTypeNumberPad;
field.borderStyle = UITextBorderStyleBezel;
field.delegate = self;
[field becomeFirstResponder];
[alert addSubview:field];
[alert show];
I looked around online to try and figure out what the problem was and some people had mentioned resigningFirst responder. I added that to - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex but it didnt do anything.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[field resignFirstResponder]
if (buttonIndex == 1) {
NSLog(@"Submit");
} else {
NSLog(@"Cancel");
}
}
I then added an if statement to try and find out if field was the first responder and i got nothing.
if([field isFirstResponder]) {
NSLog(@"field isFirstResponder");
}
does anyone have any suggestions of what i could have done wrong?
回答1:
so i think ive got it figured out.
it seems that when i was calling [field resignFirstResponder] from alertView:didDismissWithButtonIndex, firstResponder wouldnt really resign and the keyboard would be forced out a second or two after the alertView had been removed. but when i call [field resignFirstResponder] from alertView:clickedButtonAtIndex it would resign firstResponder like it is supposed to.
I also ran into a problem were the alertview would move down before being removed, to compensate for the keyboard not being there, but to fix this i created a function thats sole purpose is to call [field resignFirstResponder] and called that from alertView:clickedButtonAtIndex and delayed it being run by .2 seconds.
回答2:
Did you try removing textfield like:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[[alertView.subviews objectAtIndex:0] removeFromSuperview];
}
来源:https://stackoverflow.com/questions/4055470/wait-fences-failed-to-receive-reply-10004003