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

后端 未结 11 1346
旧巷少年郎
旧巷少年郎 2020-12-01 06:16

I\'m totally new to swift (and iOS programming at all), but I started messing around with it (it wasn\'t a good idea when everything is still beta version :D). So I tried to

11条回答
  •  难免孤独
    2020-12-01 07:12

    Swift 2.0:

    Make a sample pickerview or segment view and add it as uialercontroller's subview. Implement uipickerview delegates and present the uialertcontroller. Here is how I achieved the same.

    class ViewController: 
    
        UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
    
         var samplePicker: UIPickerView = UIPickerView()
         var sampleSegment:UISegmentedControl = UISegmentedControl ()
         var alertController:UIAlertController = UIAlertController()
         var buildings:[String] = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House","14th Street"]
    
         override func viewDidLoad() {
          super.viewDidLoad()
    
          samplePicker = UIPickerView(frame: CGRectMake(10.0, 40.0, 250, 150))
          samplePicker.delegate =  self;
          samplePicker.dataSource = self;
          samplePicker.showsSelectionIndicator = true
          samplePicker.tintColor =  UIColor.redColor()
          samplePicker.reloadAllComponents()
    
    
          sampleSegment = UISegmentedControl(items: NSArray(object: "  Dismiss ") as [AnyObject])
          sampleSegment.momentary = true
          sampleSegment.frame = CGRectMake(25, 10.0, 100.0, 30.0)
          sampleSegment.tintColor = UIColor.blackColor()
          sampleSegment.backgroundColor = UIColor.orangeColor()
          sampleSegment.addTarget(self, action: "dismissAlert", forControlEvents: UIControlEvents.ValueChanged)
    
         }
         func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    
          return 1
         }
    
         func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
          return 3
         }
    
         func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    
          return buildings[row] as String
         }
    
         func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
          print(buildings[0])
    
         }
         func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
          return 36.0
         }
         override func viewDidAppear(animated: Bool)
         {
          alertController = UIAlertController(title: " \n\n\n\n\n\n\n\n\n\n", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    
          alertController.view.addSubview(sampleSegment)
          alertController.view.addSubview(samplePicker)
    
          self.presentViewController(alertController, animated: true, completion: nil)
    
         }
         func dismissAlert()
         {
          alertController.dismissViewControllerAnimated(true, completion: nil)
         }
         override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
    
         }
        }
    

提交回复
热议问题