我正在将基于PUREMVC的应用程序从objective-c重新编码为Swift。
我现在正在创建一个表单并提交按钮,但它没有触发任何事件。既没有编辑文本字段,也没有按下按钮。文本字段上的键盘仍然显示在触摸上,但没有事件被触发。这是我的代码:
class LoginViewController: UIViewController, UITextFieldDelegate{
var emailTF: UITextField!
var passwordTF: UITextField!
var submitBTN: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.redColor()
// EMAIL
self.emailTF = ViewElements.formTextField(FORM_DEFAULT_USERNAME)
self.emailTF.tag = 100
self.view.addSubview(self.emailTF)
// PASSWORD
self.passwordTF = ViewElements.formTextField(FORM_DEFAULT_PASSWORD)
self.passwordTF.tag = 200
self.view.addSubview(self.passwordTF)
// SUBMIT
self.submitBTN = ViewElements.formButtonSubmit(FORM_SUBMIT_TEXT)
self.submitBTN.tag = 300
self.submitBTN.addTarget(self, action: "pressedButton", forControlEvents: UIControlEvents.TouchUpInside)
self.submitBTN.userInteractionEnabled = true
self.view.userInteractionEnabled = true
self.view.addSubview(self.submitBTN)
// layout
let tX = (SCREENWIDTH-NAVI_WIDTH)*0.5
let tY = SCREENHEIGHT*0.3
self.emailTF.frame = CGRect(x: tX, y: tY-NAVI_HEIGHT*1.2, width: NAVI_WIDTH, height: NAVI_HEIGHT)
self.passwordTF.frame = CGRect(x: tX, y: tY+NAVI_HEIGHT*0.2, width: NAVI_WIDTH, height: NAVI_HEIGHT)
self.submitBTN.frame = CGRect(x: tX, y: tY+NAVI_HEIGHT*1.6, width: NAVI_WIDTH, height: NAVI_HEIGHT)
// events
self.emailTF.delegate = self
self.passwordTF.delegate = self
print("---1---")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
visualEffectView.frame = self.introImage.bounds
self.introImage.addSubview(visualEffectView)
visualEffectView.alpha = 0.0
*/
// FORM EVENTS
func textFieldDidBeginEditing(textField: UITextField) {
print ("---")
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print ("---")
return false
}
func pressedButton(sender: UIButton!) {
print("::")
}
}
如果您想查看我用来创建元素的Util:
class func formTextField(myText: String) -> UITextField
{
let newTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: NAVI_WIDTH, height: NAVI_HEIGHT));
newTextField.text = myText as String
newTextField.borderStyle = UITextBorderStyle.Line
newTextField.backgroundColor = UIColor.colorWithAlphaComponent(UIColor.whiteColor())(0.5)
newTextField.textAlignment = NSTextAlignment.Center
return newTextField
}
class func formButtonSubmit(myText: String) -> UIButton
{
let newButton: UIButton = UIButton()
newButton.setTitle(myText, forState: .Normal)
newButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
newButton.titleLabel!.font = UIFont(name: "InterstateCondensed", size: 45)
newButton.titleLabel!.textColor = UIColor.blackColor()
newButton.titleLabel!.textAlignment = .Center
newButton.backgroundColor = UIColor.colorWithAlphaComponent(UIColor.whiteColor())(0.8)
newButton.frame = CGRect(x: 0, y: 0, width: NAVI_WIDTH, height: NAVI_HEIGHT)
return newButton
}
真的很奇怪,没有事件发生。任何人吗?
答案 0 :(得分:0)
您可以在调用操作时使用Selector
。
self.submitBTN.addTarget(self, action: Selector("pressedButton"), forControlEvents: UIControlEvents.TouchUpInside)
对于textField问题(我在这里猜测)问题可能是你在添加子视图后正在设置委托。
// PASSWORD
self.passwordTF = ViewElements.formTextField(FORM_DEFAULT_PASSWORD)
self.passwordTF.tag = 200
self.passwordTF.delegate = self
self.view.addSubview(self.passwordTF)
// SUBMIT
self.submitBTN = ViewElements.formButtonSubmit(FORM_SUBMIT_TEXT)
self.submitBTN.tag = 300
self.submitBTN.addTarget(self, action: "pressedButton", forControlEvents: UIControlEvents.TouchUpInside)
self.submitBTN.userInteractionEnabled = true
self.view.userInteractionEnabled = true
self.emailTF.delegate = self
self.view.addSubview(self.submitBTN)
答案 1 :(得分:0)
尝试将action: "pressedButton",
替换为action: "pressedButton:",
或尝试删除pressedButton
方法的参数,如果您不使用它。
答案 2 :(得分:0)
您忘记在:
之后添加pressedButton
,以解决您的问题:
self.submitBTN.addTarget(self, action: Selector("pressedButton:"), forControlEvents: .TouchUpInside)
同样全能的Xcode为您提供了Storyboard
,这将使您的生活更轻松,您无需从代码中设置所有参数,甚至代理和数据源也可以从中设置
答案 3 :(得分:0)
我以为我发了一个答案。问题是,我是如何在不使用故事板(我不喜欢)的情况下将viewcontroller添加到teh窗口的。以上所有代码的想法都是正确的,但不解决我的问题。我这样修好了:
window.rootViewController = rootView
window.makeKeyAndVisible()
这很简单。谢谢大家。