Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

want to show UIPickerView in UIAlertController or UIPopoverController Swift?

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/

like image 825
ibad ur rahman Avatar asked Oct 20 '15 10:10

ibad ur rahman


1 Answers

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?

like image 184
Renish Dadhaniya Avatar answered Oct 12 '22 23:10

Renish Dadhaniya