views:

86

answers:

3

EDIT: Not really a problem, I did a typo. It actually works just fine :)


Original question:

Hey.

I'm trying to place a UITextView in or on top of an UIAlertView. I know that the UIAlertView automatically uses a UITextView if message of the alert is too long, but I want it to always be an UITextView, and I want it to be editable and customizable (for style purposes).

I have tried to add a UITextView as a subview in the UIAlertView (which works for UITextFields), but that did not work with UITextView.

It seems like the UIAlertView won't accept an UITextViews as a subview.. Any way around this?

Could I put the UITextView over / on top of the UIAlertView?

I don't have time to subclass UIAlertView or create my own, but it's a good thought.

Thank you.

+2  A: 

subclass UIAlertView and build your own! A good tutorial is in this answer: stack overflow answer

Jesse Naugher
Will it still have the features of a regular UIAlertView? This is not something I will do, though, as it will take too much time.
Emil
It does not take very long to subclass, maybe 30-45 minutes to write and debug
Alex Gosselin
+1  A: 

I've added UITextFields to UIAlertViews before, and UITextView seems to work as well. Like this:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TextView!" 
                                                message:@"I have a UITextView, right here!\n\n\n\n\n"
                                               delegate:self 
                                      cancelButtonTitle:nil
                                      otherButtonTitles:@"OK", nil];

UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12, 90, 260, 50)];
myTextView.keyboardAppearance = UIKeyboardAppearanceAlert;
[alert addSubview:myTextView];
[myTextView becomeFirstResponder];
[myTextView release];

[alert show];
[alert release];

A few things to look out for:

  • Put a few \n at the end of your message, so the AlertView gets resized tall enough to fit your text field (or view)
  • Set the keyboardAppearance to UIKeyboardAppearanceAlert (otherwise the keyboard will overlap the alert)
  • Set a tag on your text field or view, so you can easily retrieve it later when the alert is dismissed.
  • Call becomeFirstResponder: on the text field (or view) so the keyboard pops up automatically

Edit: This got me thinking about putting other views in a UIAlertView. Which led to this: Stack Overflow in a UIAlertView.

zpasternack
A: 

My bad! I had accidentally allocated a UITextView-property as an UITextField, so it didn't show up correctly.

Emil