如何在iOS 7中将UITableView添加到UIAlertView中

如何在iOS 7中将UITableView添加到UIAlertView中,uitableview,ios7,uialertview,Uitableview,Ios7,Uialertview,通过谷歌搜索发现iOS 7不再支持子视图 一些ppl建议创建自定义视图,但我不确定如何才能做到这一点 这是我的密码,有人能告诉我正确的方向吗 -(IBAction)click_select_fruit_type { select_dialog = [[[UIAlertView alloc] init] retain]; [select_dialog setDelegate:self]; [select_dialog setTitle:@"Fruit Type"]; [select_dialog

通过谷歌搜索发现iOS 7不再支持子视图

一些ppl建议创建自定义视图,但我不确定如何才能做到这一点

这是我的密码,有人能告诉我正确的方向吗

-(IBAction)click_select_fruit_type
{
select_dialog = [[[UIAlertView alloc] init] retain];
[select_dialog setDelegate:self];
[select_dialog setTitle:@"Fruit Type"];
[select_dialog setMessage:@"\n\n\n\n"];
[select_dialog addButtonWithTitle:@"Cancel"];

idType_table = [[UITableView alloc]initWithFrame:CGRectMake(20, 45, 245, 90)];
idType_table.delegate = self;
idType_table.dataSource = self;
[select_dialog addSubview:idType_table];

[idType_table reloadData];

[select_dialog show];
[select_dialog release];

}

你不能。苹果不赞成在iOS 7中将任何子视图添加到
UIAlertView
中。我认为这是个好决定。许多人滥用了UIAlertView

创建自定义视图是一个好主意,但这不是您在代码中编写的内容。您似乎再次将子视图添加到
UIAlertView


请参阅。

您必须将
UIViewController
子类化,并使用其
preferredContentSize
属性执行一些模仿UIAlertView布局的“自定义模式”

您可以在iOS7的标准警报视图中将accessoryView更改为任何自己的customContentView

[alertView setValue:customContentView forKey:@"accessoryView"];
请注意,您必须在[alertView show]之前调用此选项

最简单的示例:

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
v.backgroundColor = [UIColor yellowColor];
[av setValue:v forKey:@"accessoryView"];
[av show];

Real tableView作为UIAlertView的子视图示例:


感谢链接,我看到有一个叫做UIActionSheet的东西看起来很有趣是的,这也是人们正在使用的一个解决方案。但是,如果您将UIViewController子类化以模拟UIAlertView,这也不是一个很坏的主意。环顾Github,看看是否有人做过。非常好的解决方案。看起来有点骇人,但总比试图找到UIAlertview替代方案来添加子视图要好。