7

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

我搜索了他们,但找不到任何满意的答案。我不想在UIActionSheet上这样做,因为它在iOS 9中已被弃用。我也希望它在Swift中不在Obj-C中。我尝试了下面的例子,但它不适合我。

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

+0

请审查:http://stackoverflow.com/questions/25545982/is-there-any-way-to-add-uipickerview-into-uialertcontroller-alert-or-actionshee –

+0

在上面的例子中,“Profiles”会给出错误。不为我工作。 –

+0

我的朋友,“个人资料”,“用户”是另一个来源的字段。您必须设置适当的字段或删除它们。 –

回答

2

以下代码适用于显示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 
} 
9

你Basic代码如下所示:

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?

+0

我正在努力解决它。谢谢你的回答。如果它适用于我,我会将它打勾为正确答案。 –

+0

@IbadUrRahman,Ok,NP。如果您需要任何帮助,请随时与我联系。快乐编码! –

+0

我添加选择器视图来提醒,但选择器视图不显示在警报中。选择器视图创建意志和工作没有警报,但不使用警报视图。 –

2

定义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) 
} 

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

0

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

首先,创建一个新的.xib文件,并使其成为警报的大小。 然后,放入你的图形。

现在创建一个swift文件,UIAlertView的子类并将其命名为合适的名称。

新建>文件>可可触摸子类

(或类似的东西)

让你的.xib文件,你的UIAlertView中的类名。

将您的元素从.xib拖到新的swift文件中并正确命名它们。

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

0

如果您遵循代码,您可以看到UIAlertController用于显示ActionSheets和/或警报。你不应该以艰难的方式给他们添加子视图,你将无法预测行为是否会受到影响。

也是从iOS 9开始,UIPopoverController已被弃用。但在iOS 8中,UIPopoverPresentationController和它的代表在哪里介绍,以帮助实现你想要的。 (阅读更多关于他们here

所以你可以使用它来让你的Picker显示自己作为iPad上的弹出窗口,但是如果我没有记错的话,它不会显示在iPhone上。

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

相关问题