如何在 UIAlertController 中添加 UIPickerView?

Posted

技术标签:

【中文标题】如何在 UIAlertController 中添加 UIPickerView?【英文标题】:How to add UIPickerView in UIAlertController? 【发布时间】:2015-05-04 04:09:43 【问题描述】:

任何人都可以帮助我,我不知道这样做。我有这样的课

import Foundation
import UIKit
class Alert 

    func loginAlert(viewController : UIViewController , callback: (result: Bool) -> ()) 
        var alert = UIAlertController(title : "Compose",
            message : "Fill the following",
            preferredStyle: UIAlertControllerStyle.Alert
        )


        var loginAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.Default)
            UIAlertAction in


        
        var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
            UIAlertAction in

            callback(result: false);
        
        alert.addAction(loginAction)
        alert.addAction(cancelAction)



        alert.addTextFieldWithConfigurationHandler  (textField) -> Void in
            textField.secureTextEntry = true
            textField.placeholder = "Message"

        


        viewController.presentViewController(alert, animated: true, completion: nil)

    


我读到了addTextFieldWithConfigurationHandler,它似乎只适用于文本字段,我想知道我该怎么做。任何人都可以帮助我,任何评论和建议都可以。提前致谢。

【问题讨论】:

您不能将自定义视图添加到警报控制器。 哦!所以只有文本字段在 alertview 中可用?我有什么解决办法吗? @rmaddy 您唯一的选择是找到一个自定义警报视图类型类(或编写您自己的),以进行此类自定义。 @rmaddy 很抱歉打扰你,你有什么可以看的吗? Is there any way to add UIPickerView into UIAlertController (Alert or ActionSheet) in Swift? 的可能重复项 【参考方案1】:

我需要在 AlertController(控制 NSTimer)内有一个带有取消/开始按钮的弹出 pickerView。它的样式也是。 我已经提取了所有计时器的东西,这是基本的选择器代码,希望对某人有所帮助。

ps 完整的 monty 会弹出一个选择器,它将计时器设置为所选的选择器值,并在计时器完成时播放一首曲子。有需要可以发。 它为食谱提供计时器/警报。

let pickerSet = ["5","10","15","20","30","35","40","45","50","55","60", "65", "70", "75", "80", "85", "90"]
func showPickerInActionSheet() 
    let message = "\n\n\n\n\n\n\n\n"
    let alert = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.modalInPopover = true

    let attributedString = NSAttributedString(string: "Set Timer Minutes", attributes: [
        NSFontAttributeName : UIFont.systemFontOfSize(20), //your font here,
        NSForegroundColorAttributeName : UIColor(red:0.29, green:0.45, blue:0.74, alpha:1.0) ])
    alert.setValue(attributedString, forKey: "attributedTitle")

    //Create a frame (placeholder/wrapper) for the picker and then create the picker
    let pickerFrame: CGRect = CGRectMake(35, 52, 200, 140) // CGRectMake(left, top, width, height) - left and top are like margins
    let picker: UIPickerView = UIPickerView(frame: pickerFrame)
    picker.backgroundColor = UIColor(red:0.29, green:0.45, blue:0.74, alpha:1.0)

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

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

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    alert.addAction(cancelAction)

    let okAction = UIAlertAction(title: "Start", style: .Default, handler: 
        (alert: UIAlertAction!) -> Void in self.doSomethingWithValue(Int(self.pickerSet[picker.selectedRowInComponent(0)])!) )
    alert.addAction(okAction)

    viewController.presentViewController(alert, animated: true, completion: nil)


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int  return 1 

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int  return pickerSet.count 

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?  return pickerSet[row] 

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? 
    let titleData = pickerSet[row]
    let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Verdana", size: 15.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
    return myTitle



func doSomethingWithValue(value: Int) 
    self.selection = value

【讨论】:

【参考方案2】:

你可以做到的:

let ctrl = UIAlertController(title: "title", message: "\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.Alert)

let picker: UIPickerView = ...
picker.center.x = ctrl.view.center.x
picker.center.y = ctrl.view.center.y - picker.frame.height / 2
       
let addAction = UIAlertAction(title: "ok", style:  UIAlertActionStyle.Default, handler: nil)

ctrl.addAction(addAction)
ctrl.addAction(UIAlertAction(title: I18N.load(I18N.PREFIX + "cancel"), style: UIAlertActionStyle.Cancel, handler: nil))
                    
ctrl.view.addSubview(picker)//HERE
    
view.presentViewController(ctrl, animated: true, completion: nil)

问题在于样式化视图

【讨论】:

以上是关于如何在 UIAlertController 中添加 UIPickerView?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UIAlertController 上添加动态按钮?

如何在 UIAlertController 中向 UITextField 添加边距?

Swift3:在 SKView 中添加 UIAlertController

UIAlertController - 将自定义视图添加到操作表

将 ChildViewController 添加到 UIAlertController

如何同时关闭 UIAlertController 和键盘?