传递文本字段文本数据但收到nil

时间:2015-03-16 01:46:32

标签: ios swift

我制作了一个项目并遇到了错误。我有一个弹出的视图,其中包含一个文本字段。我的目标是有人在文本字段中输入他们的名字,并计算字符串的长度,确保其足够长,并从中创建一个实例。但是,当我运行它时,每当我点击按钮时,即使我的代码中没有错误,它也会崩溃说fatal error: unexpectedly found nil while unwrapping an Optional value。请帮助谢谢。错误位于以confName()

开头的行中的countElements函数中
import UIKit
class PlayScreen : UIViewController {
@IBOutlet var scroller: UIScrollView!
@IBOutlet var PlayView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()
    scroller.contentSize.height = PlayView.frame.height
    scroller.contentSize.width = PlayView.frame.width
    scroller.userInteractionEnabled = true
}

var tutView1 : UIView!
var tutView1TextField : UITextField!
var tutView1Label : UILabel!
override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    var yPos = (PlayView.frame.height/8)
    var xPos = (PlayView.frame.width/8)
    tutView1 = UIView(frame: CGRectMake(xPos, yPos, 10, 10))
    tutView1.backgroundColor=UIColor.lightGrayColor()
    tutView1.layer.cornerRadius=25
    tutView1.layer.borderWidth=2
    tutView1.layer.borderColor = UIColor.darkGrayColor().CGColor
    self.view.addSubview(tutView1)
    UIView.animateWithDuration(1.25, animations: {
        self.tutView1.frame = CGRect(x: xPos, y: yPos, width: 10+((xPos*6)-10), height: 10+((yPos*6)-10))
        }, completion: { animationFinished in
            var tutView1Label = UILabel(frame: CGRectMake((self.tutView1.frame.width/8), (self.tutView1.frame.height/8), ((self.tutView1.frame.width/8)*6), ((self.tutView1.frame.height/4))))
            tutView1Label.text = "Welcome to your city mayor! Please give us your name and let your term begin!"
            tutView1Label.numberOfLines = 2
            tutView1Label.textAlignment = NSTextAlignment(rawValue: 1)!
            tutView1Label.font = UIFont(name: tutView1Label.font.fontName, size: 20)
            self.tutView1.addSubview(tutView1Label)
            var tutView1TextField = UITextField(frame: CGRectMake(self.tutView1.frame.width/2, self.tutView1.frame.height/2, self.tutView1.frame.width/3, self.tutView1.frame.height/10))
            tutView1TextField.frame.origin.x -= tutView1TextField.frame.width/2
            tutView1TextField.backgroundColor = UIColor.whiteColor()
            tutView1TextField.placeholder = "Name"
            tutView1TextField.layer.cornerRadius=12
            tutView1TextField.textColor = UIColor.blueColor()
            tutView1TextField.textAlignment = NSTextAlignment(rawValue: 1)!
            tutView1TextField.tintColor = UIColor.blueColor()
            self.tutView1.addSubview(tutView1TextField)

            var tutView1Button = UIButton(frame: CGRectMake(self.tutView1.frame.width/2, (self.tutView1.frame.height/4)*3, self.tutView1.frame.width/4, self.tutView1.frame.height/12))
            tutView1Button.frame.origin.x -= tutView1Button.frame.width/2
            tutView1Button.setTitle("Start your rule!", forState: UIControlState.Normal)
            tutView1Button.layer.cornerRadius = 12
            tutView1Button.tintColor = UIColor.lightGrayColor()
            tutView1Button.backgroundColor = UIColor.darkGrayColor()
            tutView1Button.titleLabel?.font = UIFont(name: tutView1Label.font.fontName, size: 13)
            tutView1Button.addTarget(self, action: "confName:", forControlEvents: UIControlEvents.TouchDown)
            self.tutView1.addSubview(tutView1Button)
    })
}

func confName(sender: UIButton!) {
    if countElements(tutView1TextField.text) >= 3 && countElements(tutView1TextField.text) <= 12 {
        var playerIns = PlayerData(name: tutView1TextField.text)
    } else {
        tutView1Label.text = "Name must be between 3 and 12 characters!"
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您声明了一个名为tutView1TextField的属性,但是在animateWithDuration的完成块中,您将创建的文本字段分配给同名的局部变量,

 var tutView1TextField = UITextField(frame: CGRectMake(self.tutView1.frame.width/2, self.tutView1.frame.height/2, self.tutView1.frame.width/3, self.tutView1.fravarme.height/10))

你应该删除&#34; var&#34;因此,您要将新文本字段分配给您的属性,而不是本地变量。

tutView1Label也存在同样的问题。