Fab*_*gue 60 iphone uiactionsheet ipad swift
目前我的ActionSheet遇到了大麻烦.在iPhone上它运行良好,但在iPad上它只会崩溃
我用一个按钮创建一个新项目
import UIKit
extension ViewController : UIActionSheetDelegate {
func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
if actionSheet.tag == 0 {
if buttonIndex == 1 {
// doing something for "product page"
} else if (buttonIndex == 2) {
// doing something for "video"
}
}
}
}
class ViewController: UIViewController, UIActionSheetDelegate {
@IBAction func test(sender: AnyObject) {
let systemVersion: NSInteger = (UIDevice.currentDevice().systemVersion as NSString).integerValue
if systemVersion < 8 {
// iOS7:
let action:UIActionSheet = UIActionSheet(title: "Change Map Type", delegate: self, cancelButtonTitle: "Back", destructiveButtonTitle: nil, otherButtonTitles: "Product Page", "Video")
action.tag = 0
action.showInView(self.view)
} else {
// iOS8:
let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cancelAction: UIAlertAction = UIAlertAction(title: "Back", style: UIAlertActionStyle.Cancel, handler: nil)
let button1action: UIAlertAction = UIAlertAction(title: "Product Page", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
// doing something for "product page"
})
let button2action: UIAlertAction = UIAlertAction(title: "Video", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
// doing something for "video"
})
alertController.addAction(cancelAction)
alertController.addAction(button1action)
alertController.addAction(button2action)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
Run Code Online (Sandbox Code Playgroud)
正如我在iphone上说的那样,但是如果我点击iPad上的按钮那么应用就会崩溃
2014-09-25 14:54:52.784 test [9541:1970048] *由于未捕获的异常'NSGenericException'而终止应用程序,原因是:'您的应用程序已呈现样式UIAlertControllerStyleActionSheet的UIAlertController().具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover.您必须通过警报控制器的popoverPresentationController为此弹出窗口提供位置信息.您必须提供sourceView和sourceRect或barButtonItem.如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供它.*第一掷调用堆栈:(0的CoreFoundation 0x00613df6 exceptionPreprocess + 182 1 libobjc.A.dylib
0x01fdaa97 objc_exception_throw + 44 2的UIKit
0x0164da37 - [UIPopoverPresentationController presentationTransitionWillBegin] + 3086 3的UIKit
0x00f54f75 __71- [UIPresentationController _initViewHierarchyForPresentationSuperview:] _ block_invoke + 1666 4的UIKit 0x00f53554 __56 - [UIPresentationController runTransitionForCurrentState] _block_invoke + 226 5的UIKit
0x00f8721b __40 + [UIViewController中_scheduleTransition:] _ block_invoke + 18 6的UIKit 0x00e4d62e ___afterCACommitHandler_block_invoke + 15 7的UIKit 0x00e4d5d9 _applyBlockToCFArrayCopiedToStack + 415 8的UIKit
0x00e4d3ee _afterCACommitHandler + 545 9的CoreFoundation
0x00536fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 30 10的CoreFoundation 0x00536f00 __CFRunLoopDoObservers + 400 11 CoreFoundation 0x0052c93a __CFRunLoopRun + 1226 12 CoreFoundation 0x0052c1ab CFRunLoopRunSpecific + 443 13 CoreFoundation
0x0052bfdb CFRunLoopRunInMode + 123 14 GraphicsServices
0x0438424f GSEventRunModal + 192 15 GraphicsServices
0x0438408c GSEventRun + 104 16 UIKit
0x00e23e16 UIApplicationMain + 1526 17 test
0x00085e9e top_level_code + 78 18 test
0x00085edb main + 43 19 libdyld.dylib
0x0273eac9 start + 1 20 ???
0x00000001 0x0 + 1)libc ++ abi.dylib:以NSException类型的未捕获异常终止
可以在https://www.dropbox.com/s/54jqd8nsc67ll5g/test.zip?dl=0找到项目进行下载和试用.
Nat*_*ook 156
错误消息告诉您需要为警报控制器提供popoverPresentationController一个位置,以便它可以正确定位.这很容易做 - 只需检查是否有弹出控制器并将发件人添加为源.
如果你的按钮是UIBarButtonItem:
if let popoverController = alertController.popoverPresentationController {
popoverController.barButtonItem = sender
}
self.presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
除此以外:
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
}
self.presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
试试这个
alertController.popoverPresentationController?.sourceView = self.view
Run Code Online (Sandbox Code Playgroud)
如果您想在 iPad 上将其显示在中间而不带箭头 [ Swift 3+ ]:
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
self.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26459 次 |
| 最近记录: |