viewcontroller.swift//Swiftui program -07import uikitclass Viewcontroller:uiviewcontroller {var clickCount:Int = 0;/ /clickcount is not declared as an int var mylabel:uilabel? Declare a global variable? Indicates that the initial value is null override func Viewdidload () {super.viewdidload ()//is a base class where some must be written keyword let color = uicolor.redcol or ();//Create a Red object//color is a constant let Color1 = Uicolor.graycolor (); Self.view.backgroundColor = Color1; Super represents the parent class//3. Create a controller//change the back color to red//Do any additional setup after loading the view, T Ypically from a nib. Adding a Uilabel//cgrect to the interface is equivalent to the interval coordinates of the previous CGRectMake//frame in the parent view//Here is the label content, and the tag in OC is taken using let Rect = CGRect (x:0,y:100,width:320,height:44); MyLabel = UILabel (Frame:rect); mylabel!. Text = "Baidu";//indicates empty also can go to create//Add MyLabel object to Self.view object Self.view.addSubview (mylabel!); mylabel!. BackgroundColor = Uicolor.redcolor (); Create a UIButton var MyButton = UIButton (Frame:cgrect (x:100,y:200,width:100,height:44)); Mybutton.backgroundcolor = Uicolor.purplecolor (); Set Text To button//settitle The first parameter does not need to be labeled, the second parameter forstate is the label mybutton.settitle ("Click Me", Forstate:. Normal); Set a text To button//to Mybuttom Add Click event Mybutton.addtarget (Self, Action: "ClickMe:", forControlEvents:. Touchupinside); Self.view.addSubview (MyButton); }//defines a click event function func clickme (Sender:uibutton) {clickcount + = 1; /** * \ (put variable or expression here) */println ("click \ (Clickcount)"); mylabel!. Text = "Baidu \ (Clickcount)"; } override func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources t Hat can be recreated. }}
Use of Swift-uiview