调用工厂类

import Foundation
import UIKit

class ViewFactory
{
    /**
     * 控件默认尺寸
     */
    class func getDefaultFrame() -> CGRect
    {
        let defaultFrame = CGRect(x: 0, y: 0, width: 100, height: 30)
        return defaultFrame
    }
    
    class func createControl(type:String, title:[String], action:Selector, sender:AnyObject)
        -> UIView {
            switch(type)
            {
            case "label":
                return ViewFactory.createLabel(title: title[0])
            case "button":
                return ViewFactory.createButton(title: title[0], action: action,
                                                sender: sender as! UIViewController)
            case "text":
                return ViewFactory.createTextField(value: title[0], action: action,
                                                   sender: sender as! UITextFieldDelegate)
            case "segment":
                return ViewFactory.createSegment(items: title, action: action, sender:
                    sender as! UIViewController)
            default:
                return ViewFactory.createLabel(title: title[0])
            }
    }
    
    /**
     * 创建按钮控件
     */
    class func createButton(title:String, action:Selector, sender:UIViewController)
        -> UIButton {
            let button = UIButton(frame:ViewFactory.getDefaultFrame())
            button.backgroundColor = UIColor.orange
            button.setTitle(title, for:.normal)
            button.titleLabel!.textColor = UIColor.white
            button.titleLabel!.font = UIFont.systemFont(ofSize: 14)
            button.addTarget(sender, action:action, for:.touchUpInside)
            return button
    }
    
    /**
     * 创建文本输入框控件
     */
    class func createTextField(value:String, action:Selector, sender:UITextFieldDelegate)
        -> UITextField
    {
        let textField = UITextField(frame:ViewFactory.getDefaultFrame())
        textField.backgroundColor = UIColor.clear
        textField.textColor = UIColor.black
        textField.text = value
        textField.borderStyle = UITextBorderStyle.roundedRect
        textField.adjustsFontSizeToFitWidth = true
        textField.delegate = sender
        return textField
    }
    
    /**
     * 创建分段单选控件
     */
    class func createSegment(items: [String], action:Selector, sender:UIViewController)
        ->UISegmentedControl
    {
        let segment = UISegmentedControl(items:items)
        segment.frame = ViewFactory.getDefaultFrame()
        //segment.segmentedControlStyle = UISegmentedControlStyle.Bordered
        segment.isMomentary = false
        segment.addTarget(sender, action:action, for:.valueChanged)
        return segment
    }
    
    /**
     * 创建文本标签控件
     */
    class func createLabel(title:String) -> UILabel
    {
        let label = UILabel()
        label.textColor = UIColor.black;
        label.backgroundColor = UIColor.white;
        label.text = title;
        label.frame = ViewFactory.getDefaultFrame()
        label.font =  UIFont(name: "HelveticaNeue-Bold", size: 16)
        return label
    }
}
import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

    var txtNum:UITextField!
    var segDimension:UISegmentedControl!
    var btn:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        makeFactor()
    }

    func makeFactor()
    {
        //创建文本标签
        let labelNum = ViewFactory.createLabel(title: "阈值:")
        labelNum.frame = CGRect(x: 20, y: 100, width: 60, height: 30)
        self.view.addSubview(labelNum)
        
        let labelDm = ViewFactory.createLabel(title: "维度:")
        labelDm.frame = CGRect(x: 20, y: 200, width: 60, height: 30)
        self.view.addSubview(labelDm)
        
        //创建文本输入框
        txtNum = ViewFactory.createTextField(value: "", action:#selector(ViewController.TextField), sender:self)
        txtNum.frame = CGRect(x:80,y:100,width:200,height:30)
        txtNum.returnKeyType = UIReturnKeyType.done
        self.view.addSubview(txtNum)
        
        //创建分段单选控件
        segDimension = ViewFactory.createSegment(items: ["3x3", "4x4", "5x5"],
                                                 action:#selector(ViewController.dimensionChanged(_:)), sender:self)
        segDimension.frame = CGRect(x:80,y: 200,width: 200,height: 30)
        self.view.addSubview(segDimension)
        segDimension.selectedSegmentIndex = 1
        
        //创建按钮控件
        btn = ViewFactory.createButton(title: "确定", action: #selector(btnClick(_ :)), sender: self)
        btn.frame.origin = CGPoint(x: 80, y: 300)
        self.view.addSubview(btn)
    }
    func TextField()  {
        print("textfield")
    }
    func dimensionChanged(_ seg:UISegmentedControl) {
        print("segDimension")
    }
    func btnClick(_ btn:UIButton)  {
        print("btn")
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //收起键盘
        txtNum.resignFirstResponder()
        //打印出文本框中的值
        print(txtNum.text as Any)
        return true
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

你可能感兴趣的:(调用工厂类)