Question

I've added UITextView as a sub view of UIAlertView, I've added UIToolBar with a Done button (using UIBarButtonItem) as inputAccessoryView to allow user to resign keyboard`. It's just working fine in iOS6.0. And not in iOS5.0.

I've break point and checked in all the way I can, for reconfirming my self, I've made a sample project and checked the same in both the iOS versions, and the problem is same.

Here's the code, that's messing with me,

-(UIToolbar *)accessoryView
{
    if (!accessoryView) {
        accessoryView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0)];

        UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(hideKeyBoard)];

        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
                                          initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                          target:nil
                                          action:nil];

        accessoryView.items = [NSArray arrayWithObjects:flexibleSpace,btn, nil];
        [accessoryView setTintColor:[[UIColor blackColor]colorWithAlphaComponent:0.5]];
    }
    return accessoryView;
}

-(IBAction) showAlertWithTextView: (id) sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
    alert.title = nil;
    alert.message = nil;
    alert.delegate = self;

    //textview I've added in .h file
    textView = [[UITextView alloc] initWithFrame:alert.bounds];
    textView.text = @"This is a UITextView";
    textView.keyboardAppearance = UIKeyboardAppearanceAlert;
    textView.editable = YES;
    textView.inputAccessoryView = [self accessoryView];
    [alert addSubview:textView];
    [alert show];
}

- (void)hideKeyBoard {
   [textview resignFirstResponder];
   //[self.view endEditing:YES]; //this is also not worked
}

Here's list of steps I'm doing,

  1. Showing Alert with Textview
  2. Focus to Textview
  3. Tap on Done button to resign TextView
  4. Its not resigning in iOS5 but resigning in iOS6

Any idea? What's going wrong?

Was it helpful?

Solution 2

I've solved it, as solution given by @iOS Developer, but as I've so many UIAlertView objects to handle, I've added a property of UIAlertView in delegate, and assigning it with the object of my alertview like this,

-(void)willPresentAlertView:(UIAlertView *)alertView
{
    //set current alertview
    [[AppDelegate sharedDelegate] setCurrentAlertView:alertView];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //will remove current alertview reference
    [[AppDelegate sharedDelegate] setNilCurrentAlertView];
}

- (void)hideKeyBoard {

    [textView resignFirstResponder];
    //when need to resign UITextView, get current alertview object
    UIAlertView *alertView = [[AppDelegate sharedDelegate] getCurrentAlertView];
    if(alertView) {
        [alertView resignFirstResponder];
    }
}

In AppDelegate.m file,

#pragma mark - UIAlertView Resign 
- (void) setCurrentAlertView:(UIAlertView *)alert{
    self.alertObj = alert;
}

- (void) setNilCurrentAlertView {
    self.alertObj = nil;
}

- (UIAlertView *)getCurrentAlertView {
    return (UIAlertView *)self.alertObj;
}

OTHER TIPS

Actually your textview is not the first responder. The first responder is your alertView.

So you should try this....

[textField resignFirstResponder];
[alertView resignFirstResponder];

To use this code you must declare your alertView object in your.h file

As it is somewhat late but I am wondering that your code will not work in iOS7 or not as from iOS7 onwards UIAlertView is not allowing to add any subview (As you are adding UITextView as subview of UIAlertView).

If your requirement is not to compulsory using the UITextView then you can use UIAlertView with following configuration with default textfield in alert.

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Title"
                                                          message:@"Message"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                                otherButtonTitles:@"Ok", nil];

        [message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];

And you can get value of this textfield in delegate of UIAlertView like this,

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
      [alertView textFieldAtIndex:0].text;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top