当从另一个类BWWalkthrough调用函数时,Properties = nil

时间:2016-03-18 12:11:53

标签: ios swift bwwalkthrough

我正在使用BWWalkthrough构建一个包含textfield的欢迎演练。

BWWalkthroughViewController中有2个预先构建的IBOutlet,我附加到我的元素(nextButtonprevButton)。基本上,我在我的页面中使用文本字段来询问他们的信息。

在我的页面类中:

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

另外,我在BWWalkthroughViewController类的ViewDidLoad中添加通知以更改按钮的位置

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 here

我还尝试直接在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调用它时,nextButtonnil,当我直接从BWWalkthroughViewController内的其他函数调用它时,它可以正常工作。< / p>

1 个答案:

答案 0 :(得分:1)

这:let nxtBtn = BWWalkthroughViewController()创建一个新对象。由于它不是屏幕显示的一部分,因此它没有加载视图,因此没有填写其插座。

您没有说如何 textfieldIsNotEmpty,但您可能希望将其传递给现有的BWWalkthroughViewController,而不是让它创建一个空的。{ / p>