以下代碼適用於顯示uipickerview的UIAertController。以前它會給iPad帶來錯誤,當我爲iPhone更正它時。它現在在兩個方面工作。 iPad和iPhone。
let alertView = UIAlertController(title: "Select Launguage", message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.ActionSheet);
if !DeviceType.IS_IPAD{
pickerView.center.x = self.view.center.x
}
alertView.view.addSubview(pickerView)
if DeviceType.IS_IPAD{
alertView.popoverPresentationController?.sourceView = self.view
alertView.popoverPresentationController?.sourceRect = self.pickerView.bounds
}
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alertView.addAction(action)
presentViewController(alertView, animated: true, completion: nil)
用於選擇設備。代碼如下所示:
enum UIUserInterfaceIdiom : Int
{
case Unspecified
case Phone
case Pad
}
struct ScreenSize
{
static let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
static let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}
struct DeviceType
{
static let IS_IPHONE_4_OR_LESS = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
static let IS_IPHONE_5 = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
static let IS_IPHONE_6 = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_6P = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
static let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
}
請審查:http://stackoverflow.com/questions/25545982/is-there-any-way-to-add-uipickerview-into-uialertcontroller-alert-or-actionshee –
在上面的例子中,「Profiles」會給出錯誤。不爲我工作。 –
我的朋友,「個人資料」,「用戶」是另一個來源的字段。您必須設置適當的字段或刪除它們。 –