最近项目中需要iPad适配,UIAlertController用的比较多,弹窗有两种模式:
<pre><code>typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } NS_ENUM_AVAILABLE_IOS(8_0);
</code></pre>
当设置UIAlertControllerStyleAlert样式的时候iPad和iPhone都可以执行,当设置UIAlertControllerStyleActionSheet的时候,iPad就会崩溃,因此可以通过设置类型判断为iPad通过UIPopoverPresentationController弹出视图:
<pre><code>`
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"FlyElephant" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
__weak typeof(self) wself = self;
UIAlertAction *action = [UIAlertAction actionWithTitle:@"FlyElephant" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleCancel handler:NULL];
[alert addAction:action];
[alert addAction:cancelAction];
if (isIPad) {
UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = self.button;
popPresenter.sourceRect = self.button.bounds;
[self presentViewController:alert animated:YES completion:nil];
}
else{
[self presentViewController:alert animated:YES completion:NULL];
}`</code></pre>