views:

6314

answers:

6

Hi, i'm having a couple of problems when i try to alloc a UITextView on an AlertView. Here is my code:

UIAlertView* minhaCritica = [UIAlertView new];
minhaCritica.title = @"7Arte";
[minhaCritica addButtonWithTitle:@"Cancelar"];
[minhaCritica addButtonWithTitle:@"Enviar"];
minhaCritica.message = @"Escreve a tua crítica:\n\n\n\n";
minhaCritica.delegate = self;
[minhaCritica show];

CGRect frame = minhaCritica.frame;
frame.origin.y -= 100.0f;
minhaCritica.frame = frame;

criticaTxtView = [UITextView alloc];
[criticaTxtView setFont:[UIFont systemFontOfSize:16.0f]];
[criticaTxtView initWithFrame:CGRectMake(20.0, 80.0, 245.0, 40.0)];
[minhaCritica addSubview:criticaTxtView];
[criticaTxtView becomeFirstResponder];

my problem is when i start inputing text, the text view doesn't scroll as i type. It should scroll one line up, when the first 2 rows are full.

Can anyone help me with this? Here's a video showing the problem: http://www.welove.com.pt/7arte.swf

Also, i'm having trouble getting the text the user inputed into a fiel. i worked with: criticaText = [[alertView textField] text]; but now it doesn't work with the code above.

A: 

For adding textfields to UIAlertViews I've been using the UIAlertView extensions. They require no extra programming on your part, and it is already part of the built in API.

First put this somewhere in your source to be able to use the addTextFieldWithValue: method.

@interface UIAlertView (Extended)
    - (UITextField*)addTextFieldWithValue:(NSString*)value label:(NSString*)label;
    - (UITextField*)textFieldAtIndex:(NSUInteger)index;
    - (NSUInteger)textFieldCount;
    - (UITextField*)textField;
@end

You don't actually have to write these methods yourself. They exist as part of the API.

And then to add text do this:

[alert addTextFieldWithValue:@"" label:@""];
DasBoot
Thanks for the reply, but i need to use a UITextView and not a textField. otherwise i cant have multi-line texting.
Filipe Guerra
I'm not sure how to do this. It might be better to not use UIAlertView for this. Design your own alertview by adding buttons and textview.
DasBoot
Using undocumented API calls could get your app rejected by Apple. Really not recommended.
Stephen Darlington
+5  A: 

I think you're trying to solve the wrong problem. According to the API docs, an Alert View is used "to display an alert message to the user." You shouldn't be using it for input. Use a modal view or something similar instead.

Following the UI conventions of a platform is generally good practice and not doing so here could get your application rejected by Apple.

Stephen Darlington
Unfortunately the “UI convention” here seems to be to display the usual blue bubble with an input field. Apple does it all the time. That’s precisely the reason all developers are trying to stick input field into UIAlertView; the only problem here is caused by Apple keeping the text field private API.
zoul
Agreed with zoul... all you have to do to see this is make an HTML page and make a link that does a Javascript "alert('...');" with a whole lotta text in the '...'. You'll see Safari do exactly what the poster is asking about.
Steve
A: 

FYI: I was rejected today.

Upon review of your application, White cannot be posted to the App Store due to the usage of private APIs. Usage of such non-public APIs, as outlined in the iPhone Developer Program License Agreement section 3.3.1, are prohibited:

"3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs."

The following non-public APIs are included in your application:

addTextFieldWithValue:label: textFieldAtIndex:

Regards, iPhone Developer Program


Jason
+1  A: 

It sounds like the relatively benign undocumented UIAlertView API's aren't being allowed anymore, so whether you're using a text field or a text label, you're going to need to use a different mechanism. I'd recommend the following blog posts are good examples of how to address this issue:

Custom UIAlertView (Color chooser)

Embedding UITables into UIAlertViews

Alert View with Prompt

These techniques appear to be legitimate from an App Store perspective and address how to embed any sorts of controls into alert views.

Ed Anuff
A: 

Sorry Jason,

we just got message from Apple that they noticed our private API calls but are not going to reject us. However, we were asked to remove the calls in the next update.

regards, marius

Marius
A: 

Made with two textfields. Look at Username and Password UITextFields in UIAlertView prompt

slatvick