简体   繁体   English

带文本字段的AlertView-初学者

[英]AlertView with textfield - beginner

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"HELOOOOOOOO" message:@"write a number of text-lines/paragraph here"
                                                      delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Yes", nil];
       UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 95, 260, 25)];
       [myTextField setBackgroundColor:[UIColor whiteColor]];
       [alert addSubview:myTextField];
       [alert show];

If you look close you will find that the message attribute is quite long. 如果您仔细观察,会发现message属性很长。 I need this alertview to first display the title, and then my long message, and then the text field and finally the 2 buttons. 我需要此Alertview首先显示标题,然后显示我的长消息,然后显示文本字段,最后显示2个按钮。

But what hapence here, is that since the message is too long, the textfield overlaps with the buttons. 但是这里发生的是,由于message太长,因此文本字段与按钮重叠。

How can i solve this ? 我该如何解决?

Implement this piece of code in your implementation file. 在您的实现文件中实现这段代码。

- (void)willPresentAlertView:(UIAlertView *)alertView
{
alertview.frame = CGRectMake(12, 95, 260, 25); //You can set a frame that suits to your needs
}

This delegate - (void)willPresentAlertView:(UIAlertView *)alertView will invoke before showing the alerview, so it is a better way to set frame to your alertview. 该委托- (void)willPresentAlertView:(UIAlertView *)alertView将在显示alerview之前调用,因此这是将frame设置为alertview的更好方法。

For more informations on aleert views: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html 有关aleert视图的更多信息: http : //developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intf/UIAlertViewDelegate http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intf/UIAlertViewDelegate

If this is for iPhone, you really can't. 如果这是针对iPhone的,那您真的不能。 The UIAlertView is not meant to handle input. UIAlertView并不打算处理输入。 Extended messages are shown with a scroll view but you really should not add a UITextField to it's view hierarchy (for one, it goes against their design standards and your app MAY get nixed!). 扩展消息以滚动视图显示,但您实际上不应该在其视图层次结构中添加UITextField(例如,它违反了其设计标准,因此您的应用程序可能会被淘汰!)。

In this situation, it would probably be best to use a new UIViewController to handle showing your content. 在这种情况下,最好使用新的UIViewController处理显示内容。

Again, the only "actions" that UIAlertView is meant to provide is that of the multiple buttons. 同样,UIAlertView旨在提供的唯一“动作”是多个按钮的动作。

Actually, it doesn't matter how long your message is. 实际上,您的信息有多长都没关系。 The alert view will always size so it fits. 警报视图将始终调整大小以适合它。

One way you can fix this is by adding a bunch of \\n at the end of your message string, which is equivalent to putting a line break. 解决此问题的一种方法是在消息字符串的末尾添加一堆\\n ,这等效于放置换行符。

EDIT: If your message is so long that it puts it in a UIScrollView , there's nothing you can really do unless you're fine with major hacking (aka, changing the bounds of the UIAlertView , moving each button down, etc.). 编辑:如果您的消息很长,以至于无法将其放入UIScrollView ,那么您将无能为力,除非您擅长进行大型黑客攻击(也就是,更改UIAlertView的范围,向下移动每个按钮等)。

A second way works only on iOS 5 and newer. 第二种方法仅适用于iOS 5及更高版本。 You can set the UIAlertView's alertStyle property to UIAlertViewStylePlainTextInput which will display an alert view with a text field. 您可以将UIAlertView's alertStyle属性设置为UIAlertViewStylePlainTextInput ,这将显示带有文本字段的警报视图。

Use newline or line break character \\n for this: 为此,请使用换行符或换行符\\n

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertView"message:@"\nhello:.................... \nwrite here.................. \nwrite here....................." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Yes", nil];
[alert show];
[alert release];

查看输出警报图像

Check this blog post for more help 查看此博客文章以获取更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM