我正在使用BWWalkthrough构建一个带有textfield的欢迎演练。
在我附加到我的元素( IBOutlets和prevButton )的BWWalkthroughViewController中有两个预构建的nextButton。基本上,我在我的页面中使用文本字段来询问他们的信息。
在我的页面中:
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的值不是零
我还试图通过直接在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中的另一个函数直接调用它时,它工作得很好。
发布于 2016-03-18 12:51:08
这是:let nxtBtn = BWWalkthroughViewController()创建一个新对象。因为它不是屏幕显示的一部分,所以它没有加载它的视图,因此也没有填充它的插座。
您并不是说是如何调用 textfieldIsNotEmpty的,但是您可能希望将它传递给现有BWWalkthroughViewController的引用,而不是让它创建一个空的BWWalkthroughViewController。
https://stackoverflow.com/questions/36084053
复制相似问题