swift--控件工厂类的实现
控件工厂类,简而言之就是,减少代码的复用率,只在哪里用,然后在哪里调:
代码如下:
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--控件工厂类的实现的更多相关文章
- 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page
[源码下载] 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page 作者:webabcd 介绍背水一战 Windows ...
- 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
[源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...
- 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment
[源码下载] 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, Vertical ...
- 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件
[源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...
- 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作
[源码下载] 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作 作者:webabcd 介绍背水一战 ...
- 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性
[源码下载] 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性 作者 ...
- 背水一战 Windows 10 (71) - 控件(控件基类): UIElement - RenderTransform(2D变换), Clip(剪裁)
[源码下载] 背水一战 Windows 10 (71) - 控件(控件基类): UIElement - RenderTransform(2D变换), Clip(剪裁) 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (70) - 控件(控件基类): UIElement - Transform3D(3D变换), Projection(3D投影)
[源码下载] 背水一战 Windows 10 (70) - 控件(控件基类): UIElement - Transform3D(3D变换), Projection(3D投影) 作者:webabcd 介 ...
- 背水一战 Windows 10 (69) - 控件(控件基类): UIElement - Manipulate 手势处理, 路由事件的注册, 路由事件的冒泡, 命中测试的可见性
[源码下载] 背水一战 Windows 10 (69) - 控件(控件基类): UIElement - Manipulate 手势处理, 路由事件的注册, 路由事件的冒泡, 命中测试的可见性 作者:w ...
随机推荐
- php phpmail发送邮件的效果
方法一: /* * 发送邮件 原 smtp ...
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- 解决Django中在.js文件中用ajax请求后端,找不到CSRF问题
function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != ...
- winform文本框不能粘贴、复制和屏蔽右键
有3个常用方法 1.MouseMove事件中,使选不中 private void textBox3_MouseMove(object sender, MouseEventArgs e) { if (t ...
- 从钉钉微应用定制化导航栏看如何实现Hybrid App开发框架
钉钉是阿里的一款企业应用APP,里面提供了混合微应用的SDK,这其实最好的一种APP架构模式.微信公众号浏览器JSSDK也提供了类似功能特性,在在交互性上没有钉钉深入. http://ddtalk.g ...
- 一站式学习Wireshark(九):应用Wireshark显示过滤器分析特定数据流(上)
介绍 掌握显示过滤器对于网络分析者来说是一项必备的技能.这是一项大海捞针的技巧.学会构建,编辑,保存关键的显示过滤器能够节省数小时的时间. 与捕捉过滤器使用的BPF语法不同,显示过滤器使用的是Wire ...
- 对设计领域中Tile和Card的理解
前端工程师离不开设计, 谈到设计就要想到大名鼎鼎的material design主题, 而material是以card为经典单元的, card即卡片, 是层次化模型的最小模块, 用于提供扁平化的信息, ...
- 我的开源主页Blog Lite配置指南
JinHengyu.github.io --- Blog Lite 0.1.1 好看的东西看多了就会不好看, 简单的东西永远不会难看 GitHub Pages 提供静态网站托管服务的厂商还是很多的, ...
- DataGridView:DataGridView控件清空绑定的数据
使用DataGridView控件绑定数据后有时需要清空绑定的数据,在清除DataGridView绑定的数据时: 1.设置DataSource为null this.dgvDemo.DataSource ...
- Hibernate- 条件查询
01.搭建开发环境 02.条件查询 package com.gordon.test; import java.util.List; import org.hibernate.Query; import ...