0

我正在使用BWWalkthrough构建一个带有文本字段的欢迎演练。

BWWalkthroughViewController我附加到我的元素(nextButtonprevButton)的内部有 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
        })
    }

当我从实际调用它时textfieldIsNotEmptynextButtonnil,当我BWWalkthroughViewController直接从内部的另一个函数调用它时,它工作得很好。

4

1 回答 1

1

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

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

于 2016-03-18T12:51:08.160 回答