加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift -UIView的使用

发布时间:2020-12-14 01:55:14 所属栏目:百科 来源:网络整理
导读://// ViewController.swift// SwiftUI程序-07import UIKitclass ViewController: UIViewController { var clickCount:Int = 0;//clickCount 并没有声明为int var myLabel:UILabel? //申明一个全局变量?表示初始值为空 override func viewDidLoad() { super.
//
//  ViewController.swift
//  SwiftUI程序-07


import UIKit

class ViewController: UIViewController {
    var clickCount:Int = 0;//clickCount 并没有声明为int
    var myLabel:UILabel? //申明一个全局变量?表示初始值为空
    override func viewDidLoad() {
        super.viewDidLoad()//是基类当中有的 必须写个关键字
        let color = UIColor.redColor();//创建一个红色对象
        //color 是一个常量
        let color1 = UIColor.grayColor();
        self.view.backgroundColor = color1;
        
        //super 表示父类
        //3.创建一个Controller 
    
        //把背静颜色改为红色
        // Do any additional setup after loading the view,typically from a nib.
   
        //在界面上加个UILabel
        //CGRect 相当于之前的CGRectMake
        //frame 在父视图中的区间坐标
        //这里是标签内容,将oc 中的标签来拿使用
        let rect = CGRect(x:0,y:100,width:320,height:44);
        myLabel = UILabel(frame: rect);
        myLabel!.text = "百度";//表示为空也可以去创建
        //把myLabel 对象加入到self.view 对象上
        self.view.addSubview(myLabel!);
        myLabel!.backgroundColor = UIColor.redColor();
        
        
        //创建一个UIButton
        var myButton = UIButton(frame: CGRect(x:100,y:200,width:100,height:44));
        
        myButton.backgroundColor = UIColor.purpleColor();
        //给button 设置文字
        //settitle 第一个参数不需要跟标签,第二个参数forstate 是标签
        myButton.setTitle("点击我",forState:.Normal);
        //给button 设置一个文字
        
        //给mybuttom 增加点击事件 
        myButton.addTarget(self,action: "clickMe:",forControlEvents:.TouchUpInside);
        self.view.addSubview(myButton);
    }
//定义一个点击事件的函数
    
    func clickMe(sender:UIButton){
        clickCount += 1;
        /**
        *  (这里放变量或者表达式)
        */
        println("click (clickCount)");
        myLabel!.text = "百度(clickCount)";
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读