UITextField inside an UIAlertView not working in iOS4.1 and works in 3.0

∥☆過路亽.° 提交于 2019-12-31 05:12:06

问题


I have a UITextField added to an UIAlerView (Kind of adding user to list with nick name, and nickname is asked by the alertview.). Thats work perfect in iOS3, But I cant type to that UITextField in iOS4.1. in iOS4.1 I do get focus to that textfield inside alertview, and keyboard appears, however typing doesn't work.

Please help in this regard.


回答1:


In iOS 4 UIAlertViews will automatically be moved and sized to avoid the keyboard if they contain any UITextFields in their subviews so you don't have to move it yourself. Simply add a text field like so:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
                                                message:@"Tap below to enter text:\n\n"
                                               delegate:nil
                                      cancelButtonTitle:@"Dismiss"
                                      otherButtonTitles:nil];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 68, 260, 30)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[alert addSubview:textField];
[textField release];
[alert show];
[alert release];

Be sure to add \n's to make room for your text field, you'll have to play around with getting the field in the correct position.




回答2:


iOS4 broke all sorts of UIAlertView hacks (er... 'customizations').

I have a custom UIAlertView replacement class on github which supports an input (with UITextField) mode. You're welcome to try it out.

https://github.com/TomSwift/TSAlertView




回答3:


I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB". To Solve your problem, you need to add a "fake" UITextField into the Alert view via coding. Please refer to the following link:

http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html



来源:https://stackoverflow.com/questions/4805637/uitextfield-inside-an-uialertview-not-working-in-ios4-1-and-works-in-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!