[英]AlertView triggering keyboard after dismissal
因此,這正在發生。
我有一個視圖控制器,它處理我應用程序中的所有警報視圖。 我有另一個視圖控制器,它具有用戶可以編輯的UITextView和一個保存按鈕。
當他們按下“保存”按鈕時,如果文本已經保存,則會觸發警報,詢問他們是否確定要更新;如果確認,它將更新文件,並給他們第二個警報,提示說成功。
發生的情況是,第二個警報出現時,鍵盤會不斷彈出。 我試過辭職鍵盤,並在按下保存按鈕后立即關閉文本字段上的用戶交互啟用標志。
self.storyEditorTextView.userInteractionEnabled=NO;
[self.storyEditorTextView resignFirstResponder];
我還試圖在響應警報時將其關閉(因為某些警報可以具有文本字段)。
更糟糕的是,當我注釋掉resign和userInteractionsEnabled行(包括警報中的那一行)時,鍵盤在第一個警報被解除后仍會出現,在第二個警報被解除后會消失(如果您可以點擊它,因為鍵盤蓋住了它),並且您無法點擊UITextView並調出鍵盤,而無需返回到父視圖。
這是警報代碼。
- (void)addPromptToFavorites
{
// throw up an alert to confirm and get a name
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Name Your Favorite!"
message:@"Would you like to add a name to your prompt?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
// add a text field
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.text = @"My Great Prompt";
// set the tag
[alert setTag:SAVE_FAVE];
// Display the alert to the user
[alert show];
}
- (void)updateFave: (NSNumber *) theFaveId
{
NSLog(@"UPDATE FAVE\n\nself.sharedFaveMan.tempFave %@",self.sharedFaveMan.tempFave);
// NSMutableDictionary *faveDict =[[NSMutableDictionary alloc] init];
// loop through the favoritePrompts array until you find a match to the faveID
for (id element in self.sharedFaveMan.favoritePrompts) {
NSNumber *valueForID = [element valueForKey:@"id"];
if([valueForID isEqualToNumber:theFaveId])
{
self.sharedFaveMan.tempFave=element;
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update Your Favorite!"
message:@"The story will be saved with the currently selected Favorite."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
//set the tag
[alert setTag:UPDATE_FAVE];
// Display the alert to the user
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UITextField *textField = [alertView textFieldAtIndex:0];
[textField resignFirstResponder];
if (buttonIndex !=0)
{
if(alertView.tag==UPDATE_FAVE)
{
NSLog(@"Updating Fave");
// loop through the favoritePrompts array until you find a match to the faveID
int counter=0;
for (id element in self.sharedFaveMan.favoritePrompts) {
NSNumber *valueForID = [element valueForKey:@"id"];
if([valueForID isEqualToNumber:self.sharedFaveMan.theFaveID]){
break;
}
counter ++;
}
// update the pieces of the prompt
[[self.sharedFaveMan.favoritePrompts objectAtIndex:counter] setObject:self.sharedFaveMan.faveStoryText forKey:@"storyText"];
// save it
[self saveFavorites];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"updateTheTable"
object:self];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success!"
message:@"Favorite Updated!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK",nil];
//play a sound
[self createSoundID: @"ticktock.aiff"];
//set the tag
[alert setTag:UPDATE_COMPLETE];
// Display the alert to the user
[alert show];
}
}
else if (buttonIndex == 0)
{
NSLog(@"%ld",(long)alertView.tag);
if(alertView.tag==SAVE_FAVE)
{
// they canceled the save
}
else if (alertView.tag==UPDATE_COMPLETE)
{
NSLog(@"Hit that");
[[NSNotificationCenter defaultCenter]
postNotificationName:@"dismissedDialogNotification"
object:self];
}
}
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
UITextField *textField = [alertView textFieldAtIndex:0];
if (textField && [textField.text length] == 0)
{
return NO;
}
return YES;
}
有任何想法嗎?
這是代碼
- (void)textViewDidEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
return true;
}
並在開始的警報方法中添加此行代碼
[self.view endEditing:YES];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.