在iOS 8中自定义UIAlertController以包含UITableView等标准元素 [英] Customize UIAlertController in iOS 8 to include standard elements like UITableView

查看:477
本文介绍了在iOS 8中自定义UIAlertController以包含UITableView等标准元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯通过[警报setValue:someView forKey:@accessoryView] 方法自定义 UIAlertViews 。这为自定义高度的UIAlertViews创建了可自定义的内容。但它只适用于iOS7及以下版本。在iOS8中, UIAlertController 已经接管,我无法再自定义它,它将削减 UIAlertView 的高度。

I am used to customize UIAlertViews through the [alert setValue:someView forKey:@"accessoryView"] method. This creates customizable content for UIAlertViews with custom heights. However it only works on iOS7 and down. In iOS8 the UIAlertController have taken over, and I cannot customize it anymore, it will cut the height of the UIAlertView.

由于滥用 UIAlertController 而不可能,或者我该怎么做?
我试图在UIAlertController中加入一个UITableView,其中包含 UIAlertControllerStyleAlert

Is it impossible because of misuse of the UIAlertController, or how am I supposed to do it? I am trying to incorporate a UITableView inside a UIAlertController with UIAlertControllerStyleAlert.

Thx。

推荐答案

我现在遇到了同样的问题。我查看了UIAlertController的私有标头( https:// github .com / nst / iOS-Runtime-Headers / blob / master / Frameworks / UIKit.framework / UIAlertController.h )并找到了一个很有前途的属性: contentViewController

I ran into the same issue right now. I looked at the private header for UIAlertController (https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIAlertController.h) and found a promising property: contentViewController

结果与UIAlertView的 accessoryView 完全相同,不同之处在于你需要将UIViewController分配给此属性而不是UIView。

And it turned out to be exactly the same as accessoryView used to be for UIAlertView, the difference being that you need to assign a UIViewController to this property rather than a UIView.

UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor redColor];

[alertController setValue:v forKey:@"contentViewController"];

这段代码将在警报视图中显示红色视图!快乐的UIAlertController定制;)

That piece of code will show a red view on the alert view! Happy UIAlertController customizing ;)

PS。这是一个私有财产,但使用KVC应该不会有问题App Store明智,我认为。

PS. It is a private property but using KVC there shouldn't be a problem App Store wise, I think.

编辑:

有些人抱怨说这不太安全。它不是公共API,所以是的,Apple可以在任何版本中更改它,导致此方法失败。

Some people complained that this isn't very safe. It's not a public API, so yes, Apple could change it in any release, causing this method to fail.

确保整个应用程序不会崩溃您可以将KVC调用包装在 try 块中。如果属性发生变化,您的控制器将不会显示内容视图,但也不会崩溃:

To make sure your entire app doesn't crash if that happens you could wrap the KVC call in a try block. If the property changes your controller won't show the content view, but it also won't crash:

@try {
    [alertController setValue:v forKey:@"contentViewController"];
}
@catch(NSException *exception) {
    NSLog(@"Failed setting content view controller: %@", exception);
}

在生产中使用此方法可能存在风险,我不推荐它重要警报。

Using this method in production can be risky, and I don't recommend it for important alerts.

这篇关于在iOS 8中自定义UIAlertController以包含UITableView等标准元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆