我在尝试将2个不同UITextView
s的字符数设置为12(例如)时遇到了一些问题。
只有一个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
}
}
答案 0 :(得分: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的内容,而不是修改其他视图的行为。