2016-01-06 52 views
0

我在嘗試將2個不同UITextView的字符數設置爲12(例如)時遇到了一些問題。如何在Swift中同時限制2個文本視圖的字符數?

它工作正常,只有一個UITextView但它變得很難與兩者。 在寫入文本時實際存在一些錯誤。它現在不輸入任何內容。我一直在嘗試不同的方式來完成它,但在shouldChangeTextInRange方法中一切看起來都是錯誤的。

如何正確設置兩個textView的字符數?

class viewControllerCuatro: UIViewController, UITextViewDelegate { 

    @IBOutlet weak var textViewInteriorUno: UITextView! 
    @IBOutlet weak var textViewInteriorDos: UITextView! 

    @IBOutlet weak var textViewExteriorUno: UITextView! 
    @IBOutlet weak var textViewExteriorDos: UITextView! 

    @IBOutlet weak var foto1: UIImageView! 
    @IBOutlet weak var foto2: UIImageView! 

    @IBOutlet weak var imagenFondo: UIImageView! 

    @IBOutlet weak var scrollView: UIScrollView! 

    override func viewDidLoad() { 

     textViewExteriorUno.delegate = self 
     textViewExteriorDos.delegate = self 
     textViewInteriorUno.delegate = self 
     textViewInteriorDos.delegate = self 


     textViewExteriorUno.layer.masksToBounds = true 
     textViewExteriorUno.layer.borderWidth = 6 
     textViewExteriorUno.layer.cornerRadius = textViewExteriorUno.layer.frame.height/6 
     textViewExteriorUno.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor 


     textViewExteriorDos.layer.masksToBounds = true 
     textViewExteriorDos.layer.borderWidth = 6 
     textViewExteriorDos.layer.cornerRadius = textViewExteriorDos.layer.frame.height/6 
     textViewExteriorDos.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor 


     foto1.layer.masksToBounds = true 
     foto1.layer.borderWidth = 6 
     foto1.layer.cornerRadius = foto1.layer.frame.height/6 
     foto1.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor 


     foto2.layer.masksToBounds = true 
     foto2.layer.borderWidth = 6 
     foto2.layer.cornerRadius = foto2.layer.frame.height/6 
     foto2.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor 

     foto1.image = fotoUnoEscogida 
     foto2.image = fotoDosEscogida 

     imagenFondo.image = UIImage (named: tipoDeHojaElegida) 
    } 


    override func viewDidAppear(animated: Bool) { 

     let alertNotSuccessRegister = UIAlertController(title: "¡Casi hemos terminado!", message: "Modifica los textos a tu gusto para que sea lo más personal posible.", preferredStyle: .ActionSheet) 

     alertNotSuccessRegister.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil)) 

     self.presentViewController(alertNotSuccessRegister, animated: true, completion: nil) 
    } 

    @IBAction func botonSiguiente(sender: AnyObject) { 
     if textViewInteriorUno.text != " " || textViewInteriorDos.text != " " { 

      textoPersonalUno = textViewInteriorUno.text! 
      textoPersonalDos = textViewInteriorDos.text! 

      performSegueWithIdentifier("hey3", sender: self) 

        print("Se acaba de guardar el texto \(textoPersonalDos)") 

     } else { 

      let alertaError = UIAlertController(title: "Por favor, rellena los campos de texto.", message: "Dedica unas bonitas palabras :D", preferredStyle: .ActionSheet) 

      alertaError.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil)) 

      self.presentViewController(alertaError, animated: true, completion: nil) 
     } 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     if textViewInteriorUno.text == "" || textViewInteriorUno.text.isEmpty == true || textViewInteriorDos.text == "" || textViewInteriorDos.text.isEmpty == true { 

      textViewInteriorUno.text = " " 
      textViewInteriorUno.resignFirstResponder() 

      textViewInteriorDos.text = " " 
      textViewInteriorDos.resignFirstResponder() 
     } 
     else{ 

      self.textViewInteriorUno.resignFirstResponder() 

      self.textViewInteriorDos.resignFirstResponder() 
     } 
    } 

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{ 

     let newLength = textViewInteriorUno.text.utf16.count + text.utf16.count - range.length 

     let otherLength = textViewInteriorDos.text.utf16.count + text.utf16.count - range.length 

     if (newLength <= 12) && (otherLength > 6) { 
      return true 
     } else { 
      return false 
     } 
    } 
+1

這些類型的用例是委託方法的第一個參數是'textView'的原因。您可以將'textView'與您的ivars進行比較並採取相應的行動。 – EmilioPelaez

回答

1

委託方法被告知哪個文本視圖的委託被調用。所以,看看方法中的textView參數,然後根據它返回。例如:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{ 

    var shouldReplace = true // For most cases, return true, only modify if we have a special case 
     let newLength = textView.text.utf16.count + text.utf16.count - range.length 
    // If the textView is 'textViewInteriorUno' 
    if textView.isEqual(textViewInteriorUno 
    { 
     shouldReplace = newLength <= 12   // will be true if the length is less than or equal to 12 
    } 
    // If the textView is 'textViewInteriorDos' 
    else if textView.isEqual(textViewInteriorDos) 
    { 
     shouldReplace = newLength < 6 // Will be true if the length is less then 6 
    } 

    return shouldReplace 
} 

這樣,你基地的委託對TextView的是什麼反應,你不修改其他意見的行爲。

+0

非常感謝兄弟。現在,它允許我鍵入第一個UITextView,但在第二個中沒有任何內容:O – FactorJose

+0

嘗試翻轉第二個>符號到<符號。我改變了代碼。 – Moshe

+0

現在很完美。謝謝@Moshe – FactorJose

相關問題