2016年12月17日 星期六

「iOS] 幫用程式碼產生的Button加上事件

之前寫過一篇  [Swift] 用程式碼在畫面上加入按鈕  ,紀錄如何用程式碼

產生UIButton在畫面上,現在我們來幫這些按鈕加上事件處理。


首先拉一下1個Label,一個TextField ,將TextFiled命名叫TelLable











完整程式碼如下:








//
//  ViewController.swift
//  myphone2
//
//  Created by boywhy chen on 2016/12/16.
//  Copyright © 2016年 boywhy chen. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var TelLable: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //用迴圈建立12個按鈕
        for var i=0;i<12;i++
        {
            //按鋌水平位置
            var X:Int = 47+(i%3) * 60
            //按鈕重直位置
            var Y:Int=95+(i/3) * 50
            
            //new 一個UIButton
            let buttonNumber:UIButton =   UIButton()
            
            //指定UIButton位置,大小
            buttonNumber.frame = CGRect(x: X , y:Y , width: 41 , height:35)
            //設定UIButton文字的顏色為白色
            buttonNumber.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
            //設定UIButton的背景色為黑色
            buttonNumber.backgroundColor = UIColor.blackColor()
            //使用系統字型,大小為22
            buttonNumber.titleLabel?.font=UIFont(name: "System", size: 22.0)
        
            //顯示按鈕的字
            if (i == 9)
            {
                buttonNumber.setTitle("DEL", forState: UIControlState.Normal)
                //del
                 //buttonNumber.addTarget(self, action: "numberDel:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonNumber.addTarget(self, action:#selector(self.numberDel), forControlEvents: UIControlEvents.TouchUpInside)
                
            }
            else if (i == 11)
            {
                buttonNumber.setTitle("CLS", forState: UIControlState.Normal)
                //cls 
                buttonNumber.addTarget(self, action:#selector(self.numberClear), forControlEvents: UIControlEvents.TouchUpInside)
            }
            else
            {
                //輸出0~9
                buttonNumber.setTitle("\(i%10)", forState: UIControlState.Normal)
                //增加數字
                buttonNumber.addTarget(self, action:#selector(self.numberClick), forControlEvents: UIControlEvents.TouchUpInside)
            }
            
            //把View加人View Control
            view.addSubview(buttonNumber)
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func numberClear(sender: AnyObject) {
        TelLable.text = ""
    }

    @IBAction func numberDel(sender: AnyObject) {
        
        var str = TelLable.text!
        
        let intValue: Int = str.startIndex.distanceTo(str.endIndex)
        
        if intValue > 0
        {
            var substr = str.substringWithRange(Range<String.Index>(start: str.startIndex.advancedBy(0), end: str.endIndex.advancedBy(-1)))
            
            
            TelLable.text = substr
        }
    }
    @IBAction func numberClick(sender: UIButton) {
        
        TelLable.text = TelLable.text! + sender.currentTitle!
    }

}


加入事件的方法:


在Swift 2使用以下這方法是有效的。

buttonNumber.addTarget(self, action:#selector(self.numberClick), 

forControlEvents: UIControlEvents.TouchUpInside)
            }


在早期的版本中,是用直接指定func的名字。

buttonNumber.addTarget(self, action: "numberDel:", 

forControlEvents: UIControlEvents.TouchUpInside)


參考資料:

Make a UIButton programatically in Swift

http://stackoverflow.com/questions/24102191/make-a-uibutton-programatically-in-swift

沒有留言:

張貼留言