前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学Swift之路(一)UI入手之基本控件

自学Swift之路(一)UI入手之基本控件

作者头像
清墨
发布2018-05-07 15:52:00
2.9K0
发布2018-05-07 15:52:00
举报
文章被收录于专栏:清墨_iOS分享清墨_iOS分享

本系列文章都是以有OC基础来写的,所以注释不会写太多,对基础不够了解的同学可能不太适合,另外本系列文章不是以教程式的形式来写,是记录学习过程的,所以不会有多少讲解

第一步:创建工程

A62A869C-6B27-46C7-96A7-77BF0FB21C66.png

创建好工程后,我们会进入这个界面

C18EA84A-89AF-4920-8824-1CF1F2CA8B8C.png

好了开始编程:写一个创建子视图的方法,里面创建一些基本的UI控件,然后在viewDidLoad中调用:

代码语言:javascript
复制
override func viewDidLoad() {
        super.viewDidLoad()
        
        self.createSubViews()
    }

    func createSubViews(){
    
       
    }

1.创建一个UILabel

代码语言:javascript
复制
//        1.UILabel
        let myLabel = UILabel(frame:CGRectMake(10,20,200,20))
        myLabel.textColor = UIColor.redColor()
        myLabel.text = "这个是一个label"
        myLabel.textAlignment = .Center;
        myLabel.font = UIFont.systemFontOfSize(15)
        myLabel.userInteractionEnabled = true;
        self.view.addSubview(myLabel)

2.UITextField

代码语言:javascript
复制
//        2.UITextField
        let myTextF = UITextField(frame: CGRectMake(50,300,100,30))
        myTextF.borderStyle = .RoundedRect
        myTextF.placeholder = "请输入文字"
        myTextF.secureTextEntry = true
        myTextF.delegate = self
        myTextF.textAlignment = .Center
        myTextF.textColor = UIColor.brownColor()
        myTextF.clearButtonMode = .WhileEditing
        myTextF.font = UIFont.systemFontOfSize(15)
        self.view.addSubview(myTextF)

这个myTextF.delegate = self写出来后,程序会报红,理由是没有签订myTextF的代理,代理直接在这里添加(","隔开)
class ViewController: UIViewController,UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.createSubViews()
    }
代码语言:javascript
复制
func textFieldShouldReturn(textField: UITextField) -> Bool {
        print("点击确定");
        textField.resignFirstResponder()
        return true
    }

3.UIButton

代码语言:javascript
复制
//        2.UIButton
        let myBtn:UIButton = UIButton(type: UIButtonType.Custom)
        myBtn.frame = CGRectMake(100, 100, 100, 100)
//        myBtn.backgroundColor = UIColor.grayColor()
        myBtn.setBackgroundImage(UIImage(named: "查公共设施"), forState: .Normal)
        myBtn.setTitle("点击按钮", forState: .Normal)
        myBtn.addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(myBtn)
代码语言:javascript
复制
func click(sender: UIButton) {
        print("%@",sender);
        
    }

4.UIImageView

代码语言:javascript
复制
//        4.UIImageView
        let myImgView = UIImageView(frame: CGRectMake(20, 20, 100, 100))
        
        let url = NSURL(string: "http://hangge.com/blog/images/logo.png")
        let data = NSData(contentsOfURL: url!)
        let img = UIImage(data: data!)
        
//        myImgView.image = UIImage(named: "查公共设施")
        myImgView.image = img;
        self.view.addSubview(myImgView)

        UIView.animateWithDuration(2) { () -> Void in
        myImgView.frame = CGRectMake(200, 200, 100, 100)
        }

写到这里,我想有OC经验的开发者已经发现,两种语言控件的属性是一样的,只是语法有些不一样而已,多写几个就会发现Swift都是一个套路.那好吧,简单的控件就不写了,接下来我们来写下UITableView

代码语言:javascript
复制
//        5.UITableView
        let myTableView = UITableView(frame: self.view.frame, style: .Plain)
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)

设置了tableView的代理,就得签订,同样,在这里签订

代码语言:javascript
复制
class ViewController: UIViewController,UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.createSubViews()
    }

有过iOS开发经验的人都知道,tableView中有两个数据源的方法是必须实现的:咱们可以command+左键点进去UITableViewDataSource看看,是这样的:

代码语言:javascript
复制
public protocol UITableViewDataSource : NSObjectProtocol {
    
    //必须的
    
    @available(iOS 2.0, *)
    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    
    @available(iOS 2.0, *)
    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    
    //可选的
    
    @available(iOS 2.0, *)
    optional public func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented
    
    @available(iOS 2.0, *)
    optional public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different
    @available(iOS 2.0, *)
    optional public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?
........................

复制粘贴这两个available的方法并实现它:

代码语言:javascript
复制
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let identifier = "swiftCell"
        var cell = tableView.dequeueReusableCellWithIdentifier(identifier)
        if (cell == nil){
        cell = UITableViewCell(style: .Default, reuseIdentifier: identifier)
            
        }
        cell?.textLabel?.text = "MySwiftCell"
        cell?.detailTextLabel?.textColor = UIColor.brownColor()
        
        return cell!
        
    }

当然,别的方法也是一样,如点击单元格:

代码语言:javascript
复制
 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("点击了单元格")
    }

好了,这篇文章的内容就到这里了,下一篇文章将会围绕UITableView,自定义UITableViewCell来写点可视化内容较强的东西。

本人也是正在学习中,文章内容如有错误,还请指正,有需要优化的地方,也请帮忙指出,帮助大家共同进步

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.02.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档