swift,精通ios中的例子

精通ios第四章的一个例子,用swift编写



//
//  ViewController.swift
//  Control Fun
//
//  Created by tujiaw on 14-9-21.
//  Copyright (c) 2014年 tujiaw. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.sliderLabel.text = "50"
        var buttonImageNormal = UIImage(named:"whiteButton.png")
        var insets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        var stretchableButtonImageNormal = buttonImageNormal.resizableImageWithCapInsets(insets)
        self.doSomethingButton.setBackgroundImage(stretchableButtonImageNormal, forState: UIControlState.Normal)
        
        var buttonImagePressed = UIImage(named: "blueButton.png")
        var stretchableButtonImagePressed = buttonImagePressed.resizableImageWithCapInsets(insets)
        self.doSomethingButton.setBackgroundImage(stretchableButtonImagePressed, forState: UIControlState.Highlighted)
        
        self.doSomethingButton.sizeToFit()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var numberField: UITextField!
    @IBOutlet weak var sliderLabel: UILabel!
    @IBOutlet weak var leftSwitch: UISwitch!
    @IBOutlet weak var rightSwitch: UISwitch!
    @IBOutlet weak var doSomethingButton: UIButton!
    
    @IBAction func textFieldDoneEditing(sender:AnyObject) {
        sender.resignFirstResponder()
    }
    
    @IBAction func backgroundTap(sender: AnyObject) {
        self.nameField.resignFirstResponder()
        self.numberField.resignFirstResponder()
    }
    
    @IBAction func sliderChanged(sender: AnyObject) {
        self.sliderLabel.text = "\(lroundf(sender.value))"
        
    }
    
    @IBAction func toggleControls(sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0 {
            self.leftSwitch.hidden = false
            self.rightSwitch.hidden = false
            self.doSomethingButton.hidden = true
        } else {
            self.leftSwitch.hidden = true
            self.rightSwitch.hidden = true
            self.doSomethingButton.hidden = false
        }
    }
    
    @IBAction func switchChanged(sender: UISwitch) {
        self.leftSwitch.setOn(sender.on, animated: true)
        self.rightSwitch.setOn(sender.on, animated: true)
    }
    
    
    @IBAction func buttonPressed(sender: AnyObject) {
        var title = "tips"
        var message = "Something was done"
        var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.ActionSheet);
        var actionOk = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default) {
            (a:UIAlertAction!) in
            println("Ok")
        }
        
        var actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
            (a:UIAlertAction!) -> Void in
            var str = "Cancel"
            println(str)
        }
        
        alert.addAction(actionOk)
        alert.addAction(actionCancel)
        self.presentViewController(alert, animated: true, nil)
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值