不知怎的,我无法让它工作(主要是因为我的n00b编程“技能”)
这是我得到的错误:[NSObject:AnyObject]在这行代码中没有名为’valueForKey’的成员:let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey)as NSValue;
我想我必须创建某种类型的IBAction,但我尝试的任何工作都没有.我需要做些什么来使代码工作?为什么我必须这样做? NSObject做什么?
这是代码:
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;}解决方法 您必须将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;}
编辑
//Add this above vIEwDIDLoad var selecteTextFIEldOriginalY:CGfloat = 0.0;//write in vIEwDIDLoad overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() NSNotificationCenter.defaultCenter().addobserver(self,name:UIKeyboardDIDShowNotification,object: nil); NSNotificationCenter.defaultCenter().addobserver(self,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; }) }总结
以上是内存溢出为你收集整理的xcode6 swift NSObject AnyObject错误全部内容,希望文章能够帮你解决xcode6 swift NSObject AnyObject错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)