想在 UIAlertController 或 UIPopoverController Swift 中显示 UIPickerView?

want to show UIPickerView in UIAlertController or UIPopoverController Swift?

我想在 UIAlertController 或 UIPopoverController 中显示 UIPickerView,我是新手 iOS,请给我一些示例代码。

我搜索了它们,但找不到满意的答案。我不想在 UIActionSheet 上这样做,因为它在 iOS 9 中已被弃用。我还希望它在 Swift 中而不是在 Obj-C 中。我试过下面的例子,但它对我不起作用。

http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/

您的基本代码如下:

let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
alert.isModalInPopover = true

//  Create a frame (placeholder/wrapper) for the picker and then create the picker
let pickerFrame = CGRect(x: 17, y: 52, width: 270, height: 100) // CGRectMake(left), top, width, height) - left and top are like margins
let picker = UIPickerView(frame: pickerFrame)

//  set the pickers datasource and delegate
picker.delegate = self
picker.dataSource = self

//  Add the picker to the alert controller
alert.view.addSubview(picker)

//Do stuff and action on appropriate object.

请查看以下答案。

Is there any way to add UIPickerView into UIAlertController (Alert or ActionSheet) in Swift?

自己定义 pickerView、数据源和委托并尝试此代码。

func createAlertView() {

    let alertView = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)

    pickerView = UIPickerView(frame: CGRectMake(0, 0, 250, 60))
    pickerView?.dataSource = self
    pickerView?.delegate = self

    alertView.view.addSubview(pickerView!)

    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

    alertView.addAction(action)
    presentViewController(alertView, animated: true, completion: nil)
}

所有其他答案都很好,我不明白为什么它不适合你。添加您的代码,也许我们可以提供帮助。

您可以制作自定义 UIAlertView 或 UIAlertViewController。这将包含您的 pickerview 以及您在自定义警报中需要的任何其他内容。

首先,制作一个新的.xib 文件,并将其设为警报的大小。 然后,放入图形。

现在制作一个 swift 文件,UIAlertView 的子class 并命名为适当的名称。

New > File > Cocoa Touch Subclass

(或类似的东西)

将您的 .xib 文件设为您的 UIAlertView 的 class 名称。

将您的元素从 .xib 拖到新的 swift 文件中并适当命名。

现在使用您的警报视图而不是默认警报视图。

如果您遵循代码,您会看到 UIAlertController 用于显示 ActionSheets and/or 警报。您不应该以困难的方式向它们添加子视图,您将无法预测行为是否会受到影响。

此外,从 iOS 9 开始,UIPopoverController 已弃用。但是在 iOS 8 中,UIPopoverPresentationController 及其委托被引入以帮助实现您想要的。 (阅读更多关于它们的信息 here

所以您可以使用它让您的 Picker 在 iPad 上显示为弹出窗口,但如果我没记错的话在 iPhone 上则不会。

底线是:最简单的方法是,制作一个您自己的自定义视图并让它实现 popover 委托协议。它应该相当简单。如果您对如何做某事有更多疑问,请在 Whosebug 中搜索此处或 post 一个新问题。

以下代码适用于显示 uipickerview 的 UIAertController。以前,当我为 iPhone 更正它时,它会给出 iPad 的错误。它现在对两者都有效。 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
}