8

I want to show UIPickerView in UIAlertController or UIPopoverController, I am new to iOS, please give me some example code here.

I searched for them but can't find any satisfactory answers. I don't want to do it on UIActionSheet, because it is deprecated in iOS 9. Also I want it in Swift not in Obj-C. I tried the below example but it is not working for me.

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

Lukesivi
  • 2,206
  • 4
  • 25
  • 43
ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40

5 Answers5

10

Your Basic Code Look Like:

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.

Please review below answer.

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

Will
  • 1,697
  • 17
  • 34
Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
  • i am trying to solve it. thanks for answer. if it work for me i will tick it as correct answer,. – ibad ur rahman Oct 20 '15 at 10:49
  • @IbadUrRahman, Ok, NP. If you need any help then feel free to contact me. Happy Coding! – Renish Dadhaniya Oct 20 '15 at 11:52
  • i add picker view to alert but picker view is not showing in alert. picker view create will and work without alert but not working with alert view. – ibad ur rahman Oct 22 '15 at 07:51
  • You should not do it this way, you are adding by hard a subview. Also you are using magic numbers to define the frame of the picker. Really consider making your own custom view. Is the best practice. Or find a third party library over at GitHub. – MikePT28 Nov 04 '15 at 17:22
2

Define pickerView, datasource and delegate yourself and try this code.

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

All other answers are good, I don't understand why it doesn't work for you. Add your code maybe we can help.

truongky
  • 1,147
  • 9
  • 12
2

below code is for UIAertController that show uipickerview. previously it give error with iPad when i make correct it for iPhone. its now work on both. iPad and 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)

for selecting device. codes are below:

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
}
ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40
0

You can make a custom UIAlertView or UIAlertViewController. This will contain your pickerview and anything else you need in your custom alert.

First, make a new .xib file, and make it the size of the alert. Then, put in your graphics.

Now make a swift file, subclass of UIAlertView and name it something appropriate.

New > File > Cocoa Touch Subclass

(or something like that)

Make your .xib file the class name of your UIAlertView.

Drag your elements from the .xib onto your new swift file and name them appropriately.

Now use your alert view instead of the default alert view.

brimstone
  • 3,370
  • 3
  • 28
  • 49
0

If you follow the code, you can see that the UIAlertController is used to display ActionSheets and/or Alerts. You should not add subviews the hard way to them, you won't be able to predict if the behavior is going to be affected.

Also as of iOS 9 the UIPopoverController is deprecated. But in iOS 8 the UIPopoverPresentationController and it's delegate where introduced to help achieve what you want. (Read more about them here)

So you could use that to make you Picker display itself as a popover on iPads, but not on iPhones if I remember correctly.

Bottom line is : The easy way is, make a your own custom view and have it implement the popover delegate protocol. It should be fairly straightforward. If you have more doubts about how to do something along the way, search here in StackOverflow or post a new question.

MikePT28
  • 51
  • 4