控件工厂类,简而言之就是,减少代码的复用率,只在哪里用,然后在哪里调:

代码如下:

import UIKit

class ViewFactory: UIView,UITextFieldDelegate {

    //默认控件的尺寸
class func getDefaultFrame( ) -> CGRect
{
let defaultFrame = CGRect(x:,y:,width:,height:)
return defaultFrame
} //类方法
class func createControl(type:String,title:[String],action:Selector,sender:AnyObject) -> UIView
{
switch type {
case "label":
return ViewFactory.creatLabel(title: title[])
case "button":
return ViewFactory.createButton(title: title[], action: action, sender: sender as! UIViewController)
case "text":
return ViewFactory.creatTextField(value: title[], action: action, sender: sender as! UIViewController as UIViewController as! UITextFieldDelegate)
case "segment":
return ViewFactory.creatSegment(items: [title[]], action: action, sender: sender as! UIViewController)
default:
return ViewFactory.creatLabel(title: title[])
}
} //创建按钮控件
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: )
button.addTarget(sender, action:action, for:.touchUpInside)
return button
} //创建文本输入框控件
class func creatTextField(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 = .roundedRect
textField.adjustsFontSizeToFitWidth = true
textField.delegate = sender
return textField
} //创建分段单选组件
class func creatSegment(items:[String],action:Selector,sender:UIViewController) -> UISegmentedControl
{
let segment = UISegmentedControl(items:items)
segment.frame = ViewFactory.getDefaultFrame()
segment.isMomentary = false
segment.addTarget(self, action: action, for: .valueChanged)
return segment
} //创建文本标签控件
class func creatLabel(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:"微软雅黑",size:)
return label }
}

调用:

 func initVIewFactory()
{
//创建文本标签
let labelNum = ViewFactory.creatLabel(title: "阈值")
labelNum.frame = CGRect(x:,y:,width:,height:)
self.view.addSubview(labelNum) let labelDm = ViewFactory.creatLabel(title: "维度")
labelDm.frame = CGRect(x:,y:,width:,height:)
self.view.addSubview(labelDm) //创建文本输入框
textNum = ViewFactory.creatTextField(value: "", action:#selector(factoryAction), sender: self as UITextFieldDelegate)
textNum.frame = CGRect(x:,y:,width:,height:)
textNum.returnKeyType = .done
self.view.addSubview(textNum) let textNumSecond = ViewFactory.creatTextField(value: "", action: #selector(factoryActionSecond), sender: self as UITextFieldDelegate)
textNumSecond.frame = CGRect(x:,y:,width:,height:)
textNum.returnKeyType = .done
self.view.addSubview(textNumSecond) //创建分段单选控件
segmentC = ViewFactory.creatSegment(items: ["3*3","4*4","5*5"], action: #selector(segmentAction), sender: self)
segmentC.frame = CGRect(x:,y:,width:,height:)
self.view.addSubview(segmentC)
segmentC.selectedSegmentIndex = //创建按钮控件
factorybtn = ViewFactory.createButton(title: "确定", action: #selector(factoryClick), sender: self)
factorybtn.frame.origin = CGPoint(x:,y:)
self.view.addSubview(factorybtn) } func factoryAction()
{ } func factoryActionSecond()
{ } func segmentAction()
{ } func factoryClick()
{
print("我点击了")
}

效果如下:

swift--控件工厂类的实现的更多相关文章

  1. 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page

    [源码下载] 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page 作者:webabcd 介绍背水一战 Windows ...

  2. 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素

    [源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...

  3. 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment

    [源码下载] 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, Vertical ...

  4. 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件

    [源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...

  5. 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作

    [源码下载] 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作 作者:webabcd 介绍背水一战 ...

  6. 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性

    [源码下载] 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性 作者 ...

  7. 背水一战 Windows 10 (71) - 控件(控件基类): UIElement - RenderTransform(2D变换), Clip(剪裁)

    [源码下载] 背水一战 Windows 10 (71) - 控件(控件基类): UIElement - RenderTransform(2D变换), Clip(剪裁) 作者:webabcd 介绍背水一 ...

  8. 背水一战 Windows 10 (70) - 控件(控件基类): UIElement - Transform3D(3D变换), Projection(3D投影)

    [源码下载] 背水一战 Windows 10 (70) - 控件(控件基类): UIElement - Transform3D(3D变换), Projection(3D投影) 作者:webabcd 介 ...

  9. 背水一战 Windows 10 (69) - 控件(控件基类): UIElement - Manipulate 手势处理, 路由事件的注册, 路由事件的冒泡, 命中测试的可见性

    [源码下载] 背水一战 Windows 10 (69) - 控件(控件基类): UIElement - Manipulate 手势处理, 路由事件的注册, 路由事件的冒泡, 命中测试的可见性 作者:w ...

随机推荐

  1. Oracle数据库字符集问题解析

    Oracle数据库字符集问题解析 经常看到一些朋友问ORACLE字符集方面的问题,我想以迭代的方式来介绍一下.第一次迭代:掌握字符集方面的基本概念.有些朋友可能会认为这是多此一举,但实际上正是由于对相 ...

  2. CPAN镜像使用帮助

    https://lug.ustc.edu.cn/wiki/mirrors/help/cpan ************************************************** 使用 ...

  3. 【算法】转载:Iterative vs. Recursive Approaches

    Iterative vs. Recursive Approaches Eyal Lantzman, 5 Nov 2007 CPOL             Introduction This arti ...

  4. eclipse memory内存溢出

    eclipse 跑web程序时候 内存溢出..解决方式 设置 jvm内存分配方案: -Xms800m -Xmx800m -XX:MaxNewSize=256m -XX:MaxPermSize=256m

  5. 汉诺塔X

    Description 1,2,...,n表示n个盘子.数字大盘子就大.n个盘子放在第1根柱子上.大盘不能放在小盘上.在第1根柱子上的盘子是a[1],a[2],...,a[n]. a[1]=n,a[2 ...

  6. QDialog:输入对话框、颜色对话框、字体对话框、文件对话框

    # _*_ coding:utf-8 _*_ import sys from PyQt4 import QtCore,QtGui class Example(QtGui.QWidget): def _ ...

  7. mount -t nfs 不能使用

    去年使用一个新的文件系统的时候,发现mount -t nfs ip:/g/ftp ~/mnt -o tcp,nolock 不能使用 一直以为是因为mount 命令更新了,有些用法我不会用,但是刚才发现 ...

  8. git中文乱码解决方案

    解决方案: 在bash提示符下输入: git config --global core.quotepath false core.quotepath设为false的话,就不会对0x80以上的字符进行q ...

  9. Android Studion的Monitor中显示No Debuggable Application的解决方法

    在使用Android Studion的时候,突然android Monitor中无法下拉显示调试项目,只是一直提示No Debuggable Application,然后上网搜索的解决办法: 第一种方 ...

  10. R中利用apply、tapply、lapply、sapply、mapply、table等函数进行分组统计

    apply函数(对一个数组按行或者按列进行计算): 使用格式为: apply(X, MARGIN, FUN, ...) 其中X为一个数组:MARGIN为一个向量(表示要将函数FUN应用到X的行还是列) ...