xcode xcode6 swift NSObject AnyObject 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24844631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:13:56  来源:igfitidea点击:

xcode6 swift NSObject AnyObject error

xcodeswiftios8xcode6

提问by etimm

I've used the code from How to move content of UIViewController upwards as Keypad appears using Swifttrying to prevent the keyboard from covering my view.

我使用了How to move content of UIViewController up as Keypad 中的代码,使用 Swift试图防止键盘覆盖我的视图。

Somehow I can't get it to work (mainly because of my n00b programming "skills") This is the error I got: [NSObject: AnyObject] does not have a member named 'valueForKey'on this line of code: let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;

不知何故我无法让它工作(主要是因为我的 n00b 编程“技能”)这是我得到的错误:[NSObject: AnyObject] does not have a member named 'valueForKey'在这行代码中:let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;

I guess I'll have to create a IBAction of some sort, but nothing I try works. What do I have to do to get the code to work? And why do I have to do it? What does NSObject do?

我想我将不得不创建某种类型的 IBAction,但我尝试的任何方法都不起作用。我需要做什么才能使代码正常工作?为什么我必须这样做?NSObject 有什么作用?

here's the code:

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self);
}

func keyboardWillShow(sender: NSNotification) {
    let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
    let rect :CGRect = s.CGRectValue();
    var frame = self.textField.frame;
    frame.origin.y = frame.origin.y - rect.height;
    self.textField.frame = frame;
}

func keyboardWillHide(sender: NSNotification) {
    let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
    let rect :CGRect = s.CGRectValue();
    var frame = self.textField.frame;
    frame.origin.y = frame.origin.y + rect.height;
    self.textField.frame = frame;
}

采纳答案by codester

You have to cast userInfo to NSDictionaryReplace above fucntions with below one

您必须将 userInfo 强制转换为NSDictionary将上述功能替换为以下功能

func keyboardWillShow(sender: NSNotification) {
    let dict:NSDictionary = sender.userInfo as NSDictionary
    let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
    let rect :CGRect = s.CGRectValue();
    var frame = self.textField.frame;
    frame.origin.y = frame.origin.y - rect.height;
    self.textField.frame = frame;
}

func keyboardWillHide(sender: NSNotification) {
    let dict:NSDictionary = sender.userInfo as NSDictionary
    let s:NSValue = dict.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
    let rect :CGRect = s.CGRectValue();
    var frame = self.textField.frame;
    frame.origin.y = frame.origin.y + rect.height;
    self.textField.frame = frame;
}

Edit

编辑

//Add this above viewDidLoad
    var selecteTextFieldOriginalY:CGFloat = 0.0;

//write in viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardDidShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardDidHideNotification, object: nil);
    }

//Replace these functions
    func keyboardWillShow(sender: NSNotification) {
        let dict:NSDictionary = sender.userInfo as NSDictionary
        let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
        let rect :CGRect = s.CGRectValue();

        var frame = self.textField.frame;
        selecteTextFieldOriginalY = frame.origin.y;

//Adjust 80 according to your need actually if is for padding and quickTypeView
        var offset = (rect.height - ((self.view.frame.height - self.textField.frame.origin.y)+self.textField.frame.size.height))+80;
        print(offset)
        frame.origin.y = offset>0 ? frame.origin.y - offset : frame.origin.y ;
        UIView.animateWithDuration(0.3, animations:{
            self.textField.frame = frame;

            }
        )
    }

    func keyboardWillHide(sender: NSNotification) {
        let dict:NSDictionary = sender.userInfo as NSDictionary
        let s:NSValue = dict.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
        let rect :CGRect = s.CGRectValue();
        var frame = self.textField.frame;
        frame.origin.y = selecteTextFieldOriginalY ;

        UIView.animateWithDuration(0.3, animations:{
            self.textField.frame = frame;

            })
    }