2

Could anyone help me I don't have any idea to do this. I have this class like

import Foundation
import UIKit
class Alert {

    func loginAlert(viewController : UIViewController , callback: (result: Bool) -> ()) {
        var alert = UIAlertController(title : "Compose",
            message : "Fill the following",
            preferredStyle: UIAlertControllerStyle.Alert
        )


        var loginAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.Default){
            UIAlertAction in


        }
        var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel){
            UIAlertAction in

            callback(result: false);
        }
        alert.addAction(loginAction)
        alert.addAction(cancelAction)



        alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
            textField.secureTextEntry = true
            textField.placeholder = "Message"

        }


        viewController.presentViewController(alert, animated: true, completion: nil)

    }

}

I read about addTextFieldWithConfigurationHandler it seems it only just for textfield, I wonder how could I do this. Could anyone help me, any comment and suggestion would do. Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
CaffeineShots
  • 2,172
  • 7
  • 33
  • 58
  • You can't add custom views to an alert controller. – rmaddy May 04 '15 at 04:17
  • ooh! so only textfield is available in alertview? do I have any work around? @rmaddy – CaffeineShots May 04 '15 at 04:20
  • 1
    Your only option is to find a custom alert view type class (or write your own) that allows for such a customization. – rmaddy May 04 '15 at 04:22
  • @rmaddy sorry for bugging you, do you have anything that i could look on? – CaffeineShots May 04 '15 at 04:31
  • possible duplicate of [Is there any way to add UIPickerView into UIAlertController (Alert or ActionSheet) in Swift?](http://stackoverflow.com/questions/25545982/is-there-any-way-to-add-uipickerview-into-uialertcontroller-alert-or-actionshee) – Sohil R. Memon May 04 '15 at 05:01

2 Answers2

1

I needed to have a popup pickerView inside an AlertController (controlling an NSTimer) with Cancel/Start buttons. It's stylable too. I've plucked out all the timer stuff, here's the basic picker code, Hope it helps someone.

ps the full monty pops up a picker which sets a timer at the selected picker value and plays a tune when timer completes. Can post it if needed. It provides a timer/alarm for a cookbook.

let pickerSet = ["5","10","15","20","30","35","40","45","50","55","60", "65", "70", "75", "80", "85", "90"]
func showPickerInActionSheet() {
    let message = "\n\n\n\n\n\n\n\n"
    let alert = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.modalInPopover = true

    let attributedString = NSAttributedString(string: "Set Timer Minutes", attributes: [
        NSFontAttributeName : UIFont.systemFontOfSize(20), //your font here,
        NSForegroundColorAttributeName : UIColor(red:0.29, green:0.45, blue:0.74, alpha:1.0) ])
    alert.setValue(attributedString, forKey: "attributedTitle")

    //Create a frame (placeholder/wrapper) for the picker and then create the picker
    let pickerFrame: CGRect = CGRectMake(35, 52, 200, 140) // CGRectMake(left, top, width, height) - left and top are like margins
    let picker: UIPickerView = UIPickerView(frame: pickerFrame)
    picker.backgroundColor = UIColor(red:0.29, green:0.45, blue:0.74, alpha:1.0)

    //set the pickers datasource and delegate
    picker.delegate = self
    picker.dataSource = self

    //Add the picker to the alert controller
    alert.view.addSubview(picker)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    alert.addAction(cancelAction)

    let okAction = UIAlertAction(title: "Start", style: .Default, handler: {
        (alert: UIAlertAction!) -> Void in self.doSomethingWithValue(Int(self.pickerSet[picker.selectedRowInComponent(0)])!) })
    alert.addAction(okAction)

    viewController.presentViewController(alert, animated: true, completion: nil)
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 1 }

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerSet.count }

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerSet[row] }

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = pickerSet[row]
    let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Verdana", size: 15.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
    return myTitle
}


func doSomethingWithValue(value: Int) {
    self.selection = value
}
Ratatat Richie
  • 131
  • 1
  • 10
0

You can do it:

let ctrl = UIAlertController(title: "title", message: "\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.Alert)

let picker: UIPickerView = ...
picker.center.x = ctrl.view.center.x
picker.center.y = ctrl.view.center.y - picker.frame.height / 2
       
let addAction = UIAlertAction(title: "ok", style:  UIAlertActionStyle.Default, handler: nil)

ctrl.addAction(addAction)
ctrl.addAction(UIAlertAction(title: I18N.load(I18N.PREFIX + "cancel"), style: UIAlertActionStyle.Cancel, handler: nil))
                    
ctrl.view.addSubview(picker)//HERE
    
view.presentViewController(ctrl, animated: true, completion: nil)

The problem is styling the view

Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
miche.atucha
  • 131
  • 2
  • 9