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

后端 未结 11 1344
旧巷少年郎
旧巷少年郎 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:20

    Swift 3.0:

    func showPickerInActionSheet() {
    
        let title = ""
        let message = "\n\n\n\n\n\n\n\n\n\n";
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.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)
        let picker: UIPickerView = 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)
    
        //Create the toolbar view - the view witch will hold our 2 buttons
        let toolFrame = CGRect(x: 17, y: 5, width: 270, height: 45)
        let toolView: UIView = UIView(frame: toolFrame)
    
    
        //add buttons to the view
        let buttonCancelFrame = CGRect(x: 0, y: 7, width: 100, height: 30) //size & position of the button as placed on the toolView
    
        //Create the cancel button & set its title
        let buttonCancel: UIButton = UIButton(frame: buttonCancelFrame);
        buttonCancel.setTitle("Cancel", for: .normal)
        buttonCancel.setTitleColor(UIColor.blue, for: .normal)
        toolView.addSubview(buttonCancel); //add it to the toolView
    
        //Add the target - target, function to call, the event witch will trigger the function call
        buttonCancel.addTarget(self, action: Selector("cancelSelection:"), for: UIControlEvents.touchDown);
    
    
        //add buttons to the view
    
        let buttonOkFrame = CGRect(x: 170, y: 7, width: 100, height: 30)//size & position of the button as placed on the toolView
    
        //Create the Select button & set the title
        let buttonOk: UIButton = UIButton(frame: buttonOkFrame);
        buttonOk.setTitle("Select", for: UIControlState.normal);
        buttonOk.setTitleColor(UIColor.blue, for: UIControlState.normal);
        toolView.addSubview(buttonOk); //add to the subview
    
        buttonOk.addTarget(self, action: #selector(HomeViewController.saveDelayTime), for: UIControlEvents.touchDown);
    
        //add the toolbar to the alert controller
        alert.view.addSubview(toolView);
    
        self.present(alert, animated: true, completion: nil);
    }
    
    func saveProfile(sender: UIButton){
        // Your code when select button is tapped
    
    }
    
    func saveUser(sender: UIButton){
        // Your code when select button is tapped
    }
    
    func cancelSelection(sender: UIButton){
        self.dismiss(animated: true, completion: nil)
    
        // We dismiss the alert. Here you can add your additional code to execute when cancel is pressed
    }
    
    // returns number of rows in each component..
    func numberOfComponents(in pickerView: UIPickerView) -> Int{
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 60
    }
    
    // Return the title of each row in your picker ... In my case that will be the profile name or the username string
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "\(row)"
    
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedTime = row
    }
    
    func saveDelayTime() {
        self.dismiss(animated: true, completion: nil)
        UserDefaults.standard.set(selectedTime, forKey: "DelayTimeKey")
        let _ = UserDefaults.standard.synchronize()
    }
    

提交回复
热议问题