首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性=零当函数从另一个类BWWalkthrough调用时

属性=零当函数从另一个类BWWalkthrough调用时
EN

Stack Overflow用户
提问于 2016-03-18 12:11:52
回答 1查看 113关注 0票数 0

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

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

在我的页面中:

代码语言:javascript
复制
class WTnameViewController: UIViewController, UITextFieldDelegate, BWWalkthroughPage {

我添加了一个观察者来查看文本字段何时更改。

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    //  ...
  NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textfieldIsNotEmpty:"), name:UITextFieldTextDidChangeNotification, object: textfieldName)
    //  ...
}

它称这个函数为:

代码语言:javascript
复制
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类的通知,以更改按钮的位置。

代码语言:javascript
复制
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillChangeFrameNotification, object: nil)

职能如下:

代码语言:javascript
复制
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中创建了以下函数:

代码语言:javascript
复制
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中的另一个函数直接调用它时,它工作得很好。

EN

回答 1

Stack Overflow用户

发布于 2016-03-18 12:51:08

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36084053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档