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 a code example here.

I searched for them, but did not find satisfactory answers. I don’t want to do this on UIActionSheet because it is deprecated in iOS 9. Also I want it to be in Swift not in Obj-C. I tried the example below, but it does not work for me.

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

+7
ios swift uialertcontroller uipopovercontroller uipickerview
source share
5 answers

below code is for UIAertController which shows uipickerview. He previously reported an iPad error when I fixed it for the iPhone. Now he is working 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) 

to select a device. codes 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 } 
+2
source share

Your base code is as follows:

 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. 

Read the answer below.

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

+9
source share

Define pickerView, data source 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 do not understand why it does not work for you. Add your code, maybe we can help.

+2
source share

You can create a custom UIAlertView or UIAlertViewController. This will contain your pickerview and everything you need in your custom alert.

First create a new .xib file and make it a size for warning. Then insert your schedule.

Now make a quick file, subclass UIAlertView and name it something suitable.

New> File> Cocoa Touch Subclass

(or something like that)

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

Drag and drop elements from .xib into a new fast file and name them accordingly.

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

0
source share

If you execute the code, you will see that the UIAlertController is used to display ActionSheets and / or warnings. You do not have to add subviews to the difficult path to them; you cannot predict whether behavior will be affected.

As with iOS 9, the UIPopoverController is deprecated. But in iOS 8 there is a UIPopoverPresentationController, and it delegates to where it is presented to help achieve what you want. (More about them here )

So you can use this so that you yourself display Picker as a popover on iPads, but not on iPhone, if I remember correctly.

Bottom line: An easy way is to create your own custom view and implement it using the delegate protocol. It should be pretty simple. If you have more doubts about how to do something along this path, search here at StackOverflow or submit a new question.

0
source share

All Articles