swift 基础学习一

时间:2021-03-04 22:24:06

1. 利用NSUserDefaults存储用户名和密码

(1)获取用户名和密码

 userTextFeild.text = NSUserDefaults.standardUserDefaults().valueForKey("userName") as! String!
passWordTextFeild.text = NSUserDefaults.standardUserDefaults().valueForKey("password") as! String!
(2)存储用户名和密码

// MARK: 点击事件
func loginButtonClick(sender: UIButton!)
{
print("UISlider Vaule: \(sender.tag)")

if userTextFeild.text == "ww" && passWordTextFeild.text == "123456"
{
NSUserDefaults.standardUserDefaults().setObject(userTextFeild.text, forKey: "userName")
NSUserDefaults.standardUserDefaults().setObject(passWordTextFeild.text, forKey: "passWord")
NSUserDefaults.standardUserDefaults().synchronize()

let rootVC = TFTabBarViewController()
self.navigationController!.pushViewController(rootVC, animated: false)
}

else
{
let alertView = UIAlertView()
alertView.title = "提示"
alertView.message = "账户或密码输入错误"
//alertView.addButtonWithTitle("我知道了")
alertView.addButtonWithTitle("确定")
alertView.cancelButtonIndex=0
alertView.delegate=self;
alertView.show()
}
}


2. UIButton  初始化

 let loginButton = UIButton(frame: CGRectMake(10, 240, 300, 40))
loginButton.setTitle("登录", forState:UIControlState.Normal)
loginButton.backgroundColor = UIColor.redColor()
loginButton.tag = 999
loginButton.addTarget(self, action: "loginButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(loginButton)

3. UIImageView初始化,以及在它上面添加图片

 let backButton = UIButton(frame: CGRectMake(0, 12, 70, 44))
backButton.addTarget(self, action: "backBtn:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(backButton)

let arrowView=UIImageView(image:UIImage(named:"icon-driver-zhaohuo-back.png"))
arrowView.frame=CGRectMake(0, 12, 50, 35)
backButton.addSubview(arrowView)

4. UILabel初始化

 titleLable = UILabel(frame: CGRectMake(0,25,self.view.frame.size.width, 30))
titleLable.text = title as String
titleLable.textAlignment = NSTextAlignment.Center
titleLable.font=UIFont(name:"Thonburi",size:18)
titleLable.backgroundColor = UIColor.clearColor()
self.navigationView.addSubview(titleLable);