如何在iOS中将UITextView插入UIAlertview

9

另外,我想要将这个UITextView与XXX.txt文件中的文本一起使用。在iOS6中它可以正常工作,但在iOS7上,内容不会出现在UIAlertview中。我的代码如下:

UITextView *tosTextView = [[UITextView alloc]initWithFrame:CGRectMake(12, 48, 260, 140)];

NSString *path = [[NSBundle mainBundle] pathForResource:@"TJTermsof Service"
                                                 ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];
[tosTextView setText:content];

tosTextView.textColor=[UIColor blackColor];
tosTextView.font = [UIFont fontWithName:@"LuzSans-Book" size:17];
tosTextView.editable = NO;
customAlert = [[UIAlertView alloc]initWithTitle:@" TOS \n\n\n\n\n\n " message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"I Agree",nil];



[customAlert addSubview:tosTextView];

[customAlert show];
4个回答

6

这应该可以工作。试试看。但这是一种hack方法,可能不会得到苹果公司的赞赏。

UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:title
                                                    message:@""
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Done", nil];
UITextView *textView = [UITextView new];
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))//For Backward compatibility 
{
    [testAlert addSubview: textView];
}
else
{
    [testAlert setValue: textView forKey:@"accessoryView"];
}
[testAlert show];

如何获取TextView的值? - Gank
@Gank,如果你需要使用它,你需要为textview创建一个outlet。valueForKey:@"accessoryView"似乎不起作用。但是对于低于iOS7的SDK,你可以从警报视图中获取所有子视图。 - Koushik Ravikumar
@KoushikKumar你能解释一下如何为TextView创建插座吗? - scientiffic
@KoushikKumar 我该如何在TextView中添加占位符? - Muju

4

3

在iOS7中,您无法修改警报视图的视图层次结构。

您需要自己实现一个警报视图,或使用开源实现,例如SDCAlertView


我不需要第三方。 - Anbu.Karthik
@Anbu.Karthik 那你就无法实现。 - Léo Natan
每一个代码,我都会编写在程序中,因为我只有两个月的工作经验,需要学习,我在iOS方面还是一个小孩。 - Anbu.Karthik
使用开源没有任何问题。更好的是,看一下代码,你会学到新东西。 - Léo Natan
HMM,你是资深的,你说得没错,好吧朋友,我使用SDCAlertView。 - Anbu.Karthik
显示剩余4条评论

2

我已经亲自测试过了。

默认情况下,UIAlertview的内容是一个文本视图。您提供的内容越多,UIAlertview中的文本视图就会呈现得更大。

我认为您在Alertview标题中提供了文本。您应该将其放在消息中。

enter image description here


谢谢,但文本文件包含一些表单格式,所以我才问的。 - Anbu.Karthik
您给出的输出直接将一些文本内容插入到UIAlertview中,但我需要将附加的txt文件插入到textview中,并且将textview插入到UIAlertvie的子视图中。 - Anbu.Karthik
好的,明白了。那么最好的方法是将它推入一个带有文本视图的新视图控制器中。如果你要显示一个文件,那么带有UITextView的视图控制器是最好的选择。 - ipraba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接