我正在使用BWWalkthrough构建一个带有文本字段的欢迎演练。
BWWalkthroughViewController
我附加到我的元素(nextButton
和prevButton
)的内部有 2 个预构建的 IBOutlets 。基本上,我在我的页面中使用文本字段来询问他们的信息。
在我的页面类内:
class WTnameViewController: UIViewController, UITextFieldDelegate, BWWalkthroughPage {
我添加了一个观察者来查看文本字段何时更改。
override func viewDidLoad() {
super.viewDidLoad()
// ...
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textfieldIsNotEmpty:"), name:UITextFieldTextDidChangeNotification, object: textfieldName)
// ...
}
它调用这个函数:
func textfieldIsNotEmpty(notification: NSNotification) {
// BWWalkthroughViewController is the master class.
let nxtBtn = BWWalkthroughViewController()
if textfieldName.text?.isEmpty == true {
// Animate the fade-out of the button
UIView.animateWithDuration(0.4, animations: { () -> Void in
nxtBtn.nextButton?.layer.opacity = 0
}, completion: { finished in
nxtBtn.nextButton?.hidden = true
})
} else if textfieldName.text?.isEmpty == false {
// animate the fade-in of the button in BWWalkthrough
UIView.animateWithDuration(0.4, animations: { () -> Void in
nxtBtn.nextButton?.hidden = false
nxtBtn.nextButton?.layer.opacity = 1
}, completion: { finished in
})
}
}
但是按钮是一个
nil
。
另外,我在类的 ViewDidLoad 中添加了一个通知BWWalkthroughViewController
来更改按钮的位置
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillChangeFrameNotification, object: nil)
和功能:
func keyboardNotification(notification: NSNotification) {
// Below we define the animation
let userInfo = notification.userInfo!
let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let convertedKeyboardEndFrame = view.convertRect(keyboardEndFrame, fromView: view.window)
let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntValue << 16
let animationCurve = UIViewAnimationOptions(rawValue: UInt(rawAnimationCurve))
// I change the constraint of the button with the animation
buttonNextBottomConstraint!.constant = CGRectGetMaxY(view.bounds) - CGRectGetMinY(convertedKeyboardEndFrame)
buttonBackBottomConstraint!.constant = CGRectGetMaxY(view.bounds) - CGRectGetMinY(convertedKeyboardEndFrame)
UIView.animateWithDuration(animationDuration, delay: 0.0, options: [.BeginFromCurrentState, animationCurve], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
// Here I check if 'nextButton' is a nil - Debug only
print("keyboard : \(nextButton)")
}
这里的值
nextButton
不是nil
我还尝试通过直接在内部的函数更改按钮,BWWalkthroughViewController
但它也返回给我一个nil
.
有人可以解释一下为什么nextButton
一个函数是零而不是另一个?
编辑 :
我在中创建了以下函数BWWalkthroughViewController
:
public func textfieldChangeButton() {
// Do stuff here when receive a notification about a textfield
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.nextButton?.hidden = false
self.nextButton?.layer.opacity = 1
print("textfieldChangeButton : \(self.nextButton)")
}, completion: { finished in
})
}
当我从实际调用它时textfieldIsNotEmpty
,nextButton
是nil
,当我BWWalkthroughViewController
直接从内部的另一个函数调用它时,它工作得很好。