2015-12-22 16 views

risposta

8

Questo è un bene appraoch ...

func popUpController() 
{ 

    let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) 

    let margin:CGFloat = 8.0 
    let rect = CGRectMake(margin, margin, alertController.view.bounds.size.width - margin * 4.0, 100.0) 
    let customView = UITextView(frame: rect) 

    customView.backgroundColor = UIColor.clearColor() 
    customView.font = UIFont(name: "Helvetica", size: 15) 



    // customView.backgroundColor = UIColor.greenColor() 
    alertController.view.addSubview(customView) 

    let somethingAction = UIAlertAction(title: "Something", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in print("something") 

     print(customView.text) 

    }) 

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {(alert: UIAlertAction!) in print("cancel")}) 

    alertController.addAction(somethingAction) 
    alertController.addAction(cancelAction) 

    self.presentViewController(alertController, animated: true, completion:{}) 


} 
+3

Ho fatto qualche ricerca su questo. Sembra che l'implementazione interna di UIAlertController sia cambiata (sto lavorando su IOS9) e questo non funzionerà. Solo FYI. –

0

Non è possibile creare testo a più righe in UIAlertController. Devi creare il tuo UIAlertController per quello.

+0

Grazie Questo è un buon approccio per fare il mio costume, finalmente ho renderlo come di seguito nella mia risposta. Grazie mille dammi un buon collegamento. – Vishal

2

ho fatto un paio di modifiche per l'approccio di Vishal.

  1. Aggiunto il metodo UITextViewDelegate per visualizzare un testo grigio chiaro iniziale come segnaposto e rimuoverlo su textViewDidBeginEditing.
  2. Imposta la larghezza del bordo del testo, il colore del bordo e il colore dello sfondo per renderlo visibile.

    class ViewController: UIViewController, UITextViewDelegate { 
    
    ... 
    
    @IBAction func showAlert(sender: AnyObject) { 
        let alertController = UIAlertController(title: "Hello, I'm alert! \n\n\n\n\n\n\n", message: "", preferredStyle: .Alert) 
    
        let rect  = CGRectMake(15, 50, 240, 150.0) 
        let textView = UITextView(frame: rect) 
    
        textView.font    = UIFont(name: "Helvetica", size: 15) 
        textView.textColor   = UIColor.lightGrayColor() 
        textView.backgroundColor = UIColor.whiteColor() 
        textView.layer.borderColor = UIColor.lightGrayColor().CGColor 
        textView.layer.borderWidth = 1.0 
        textView.text    = "Enter message here" 
        textView.delegate   = self 
    
        alertController.view.addSubview(textView) 
    
        let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 
        let action = UIAlertAction(title: "Ok", style: .Default, handler: { action in 
    
         let msg = (textView.textColor == UIColor.lightGrayColor()) ? "" : textView.text 
    
         print(msg) 
    
        }) 
        alertController.addAction(cancel) 
        alertController.addAction(action) 
    
        self.presentViewController(alertController, animated: true, completion: {}) 
    
    } 
    
    func textViewDidBeginEditing(textView: UITextView) { 
        if textView.textColor == UIColor.lightGrayColor(){ 
         textView.text = "" 
         textView.textColor = UIColor.darkGrayColor() 
        } 
    } 
    } 
    
+0

Grazie mille. È esattamente come volevo. Ma non salta quando viene avviata la modifica. Hai una soluzione per questo. –

Problemi correlati