ISO Swift Code First

Source: Internet
Author: User
Tags uikit

http://study.163.com/course/courseLearn.htm?courseId=873007#/learn/video?lessonId=1049033&courseId=873007


Empty Application


Appdelegate.swift


var Window:uiwindow?


Self.window = UIWindow (Frame:UIScreen.mainScreen (). Bounds)
self.window!. BackgroundColor = Uicolor.whitecolor ()
self.window!. Makekeyandvisible ()




Create a Controller
var RVC = Rootviewcontroller ()
self.window!. Rootviewcontroller = RVC
return True








Rootviewcontroller.swift
Import Foundation;
Import UIKit;


Class rootviewcontroller:uiviewcontroller{
Override Func Viewdidload () {
Super.viewdidload ();
Add a uilable to the interface
Let rect = CGRect (x:0,y:100,width:320,height:44)
Create a Uilable object
var MyLabel = UILabel (frame:rect)
To set a text on a label
MyLabel.Text = "Striker iOS"
Set a background color for a label
Mylabel.backgroundcolor = Uicolor.redcolor ()
Self.view.addSubview (MyLabel)

var MyButton = UIButton (Frame:cgrect (x:100,y:100,width:100,height:100))
Mybutton.settitle ("Click Me", Forstate:.) Normal)
Mybutton.backgroundcolor = Uicolor.bluecolor ()
Add a click event to MyButton
Mybutton.addtarget (self,action: "ClickMe:", forControlEvents:. Touchupinside)
Self.view.addSubview (MyButton)
}
}


var button = UIButton (Frame:cgrect (x:0,y:0,width:100,height:100))
Button.settitle ("Hello", forcontrolevents:. Normal)
Button.addtarget (self,action: "ClickMe", forControlEvents:. Touchupinside)
Button.backgroundcolor= Uicolor.bluecolor ()
Self.view.addSubview (Button)






2 uinavigation


Let Rcv:uiviewcontroller = Rootviewcontroller ()
Creating a navigation controller
Let Nav = Uinavigationcontroller (ROOTVIEWCONTROLLER:RCV)
self.window!. Rootviewcontroller = nav;


Rootviewcontroller.swift
Import Foundation
Import UIKit


Class rootviewcontroller:uiviewcontroller,fontsizechangedelegate{
Override Func Viewdidload ()
{
Self.title = "title"
Put a button on the right Uibarbuttonitem
Let Nextitem = Uibarbuttonitem (title: "Next Page", Style:.plain,
Target:self,action: "NextPage")
Self.navigationItem.rightBarButtonItem = Nextitem

Define a label
var MyLabel = UILabel (Frame:cgrect (x:1,y:1,width:100,height:100))
mylabel!. Text = "Hello World"
Self.view.addSubview (MyLabel)
}

Func nextPage ()
{
NSLog ("clicked on NextPage")
Let svc = Secondviewcontroller ()
Svc.delegate = Self
Self.navigationController.pushView (Svc,animated:true)
}

Func Fontsizedidchange (Controller:secondviewcontroller, Fontsize:int)
{
Let font = Uifount.systemfontofsize (Float (fontSize))
mylabel!. Font = Font
}
}


Secondviewcontroller.swift
Import Foundation
Import UIKit


Protocol fontsizechangedelegate:nsobjectprotocol{
Defines a protocol function, a function of a proxy
Parameter 1 Current controller itself
Parameter 2 font size
Func Fontsizedidchange (Controller:secondviewcontroller,fontsize:int)
}
Class secondviewcontroller:uiviewcontroller{
var fontsize:int = 20
Define a delegate object, write the Protocol object
var delegate:fontsizechangedelegate?
Override Fun Viewdidload ()
{
Super.viewdidload ()

Self.title = "Second page"
Self.view.backgroundColor = Uicolor.redcolor ()

var B = Uibutton.buttonwithtype (. System) as UIButton
var frame = CGRect (x:100,y:100,width:100,height:40)
B.frame = Frame
B.settitle ("Return to previous layer", forstate:.) Normal)
B.addtarge (self,action: "ClickMe:", Forcontrollerevent:. Touchupside)
Self.view.addSubview (b)

}
Func ClickMe (Sender:uibutton)
{
FontSize + +;
if (DELEAGTE)
{
Delegate?. Fontsizedidchange (Self,fontsize:fontsize)
}
Return to the previous layer
Self.navigationController.popViewControllerAnimated (True)
}


}














































































































,

ISO Swift Code First

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.