The Swift Code之项目开发BUG收录,持续更新......

吴统威 on 编程语言 SWIFT | 2015-04-10 11:14:22.0

这篇文章是作为自己本身在开发项目中遇到问题的一个笔记,以便以后方便参考!


1.在使用不同的storyborad文件的视图跳转的时候,请不要将代码写在viewDidLoad,viewWillAppear中,否则出现警告,不能跳转

Warning: Attempt to present <UINavigationController: 0x7fb799c13650> on <sqt_ios.MainTabBarController: 0x7fb79843ce50> whose view is not in the window hierarchy!

可以将代码写在viewDidAppear方法中

 override func viewDidAppear(animated: Bool) {
     let st = UIStoryboard(name: "loginreg", bundle: nil)
     let controller = st.instantiateViewControllerWithIdentifier("login") as! UINavigationController
                
     self.presentViewController(controller, animated: true, completion: nil)
 }


2.在xcode中,拖入UITextView,文本内容不会从顶部开始排版,而是直接垂直居中显示

D64D38B3-E25A-4B8B-B12C-255F407029B4.png

这样是我不需要的,我需要的时从顶部开始输入,这时候我们可以在xcode中将视图的属性栏中的layout 的adjust Scroll View  insets去掉勾选就可以了

B218B101-4726-4EDA-B9DB-00AC1B1873FA.png

或者在当前的controller中加入以下代码

self.automaticallyAdjustsScrollViewInsets = false


3.如果想要将UITextField的delegate设置为UITextFieldDelegate的子类,而不是当前的Controller,需要先声明为weak,否则报错

weak var pd = PhoneDelegate()
class PhoneDelegate:NSObject,UITextFieldDelegate {
    func textFieldDidEndEditing(textField: UITextField) {
        
        NSLog("this")
    }
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        NSLog("this is")
         return true
    }
   
}

4.有时候会遇处理小数点留位数的问题,今天找到swift处理的小数点位数,显示为字符串

let avgTemp = 66.844322156
println(NSString(format:"%.2f", avgTemp))

5.获取APP应用中的rootViewController,以及获取navigationController的子视图

UIApplication.sharedApplication().keyWindow?.rootViewController //获取APP的rootViewController

self.navigationController.viewControllers //获取navigation下的所有子视图

未完待续,陆续更新中................