With the release of Swift's new development language, And with the official release of Xcode6.0.1, the use of Swift to write iOS code is imminent, the author in the use of objective-c developed nearly three years, the elegant syntax is deeply praised, the following I will be a comparison of the implementation of a page value of the demo, using the syntax is swift, the page value is learning iOS early must Repair of the demo, because involved a very difficult to understand the syntax: the agreement and the delegation, here is involved in the swift syntax and some basic operations I do not repeat in one by one, if it is convenient to download the IT interview app, there is a detailed introduction to it, then go straight to the point, with code to achieve the following functions:
1, create Swift project, can use Xib or pure code, here main pure code, people will notice this strange language without. h file, but with. Swift files
2, modify the phone screen display name, this and XCODE5 serious difference, do not know is backward or forward, the author said very silent, back to see how to restore it
3, basic Swift syntax to define: protocol, forward value and reverse value, i.e. callback
4, simple to mention how to change the xib default to pure code, that is, delete xib file
5, the page function is the first entry number, and then click on the registration, modal rendering registration page, this shows the previous page fill in the number, then enter the name and click OK, return to the last page display name
So much, the following concrete implementation, if the Novice can follow the steps to do, encountered the grammar of the understanding, please Baidu:
First, create the project, the overall code structure such as:
I'm not saying anything. , into into the next
Two, pure Code Building first page (Homeviewcontroller.swift) and Registration page (Registerviewcontroller.swift)
class HomeViewController: UIViewController,RegisterDelegate {
var nameLbl : UILabel!
var numTF : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.whiteColor()
let titleItem : UINavigationItem = UINavigationItem(title: "首页")
let NVC : UINavigationBar = UINavigationBar(frame: CGRectMake(0, 20, 320, 44))
NVC.setItems([titleItem], animated: true)
self.view.addSubview(NVC)
numTF = UITextField(frame: CGRectMake(10, 100, 300, 35))
numTF.placeholder = "输入学号"
numTF.borderStyle = UITextBorderStyle.Line
numTF.textAlignment = NSTextAlignment.Center
numTF.clearButtonMode = UITextFieldViewMode.WhileEditing
self.view.addSubview(numTF)
nameLbl = UILabel()
nameLbl.frame = CGRectMake(10, 150, 300, 40)
nameLbl.text = ""
nameLbl.backgroundColor = UIColor.lightGrayColor()
nameLbl.textAlignment = NSTextAlignment.Center
self.view.addSubview(nameLbl)
let registerBtn : UIButton = UIButton()
registerBtn.frame = CGRectMake(10, 200, 300, 40)
registerBtn.backgroundColor = UIColor.lightGrayColor()
registerBtn.setTitle("注册", forState: UIControlState.Normal)
registerBtn.addTarget(self, action: "registerClick:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(registerBtn)
}
Notice the navigation bar code, as well as various UI code, very wonderful writing, the current author only suitable for 320, did not do anything else, we can adapt, so that a home UI is created, and then implement the Click event:
func goRegister () {
if numTF.text.isEmpty {
var alert: UIAlertView = UIAlertView (title: "Cannot be empty", message: "Fill in your student ID", delegate: nil, cancelButtonTitle: "I know")
alert.show ()
numTF.becomeFirstResponder ()
} else {
var rootVC: RegisterViewController = RegisterViewController ()
let NVC: UINavigationController = UINavigationController (rootViewController: rootVC)
self.presentViewController (NVC, animated: true, completion: nil)
}
}
This is the registration page (registerviewcontroller.swift), now paste the registration page code:
class RegisterViewController: UIViewController, UITextFieldDelegate {
var nameTF: UITextField!
var num: String!
override func viewDidLoad () {
super.viewDidLoad ()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.whiteColor ()
self.title = "Register"
let leftItem: UIBarButtonItem? = UIBarButtonItem (title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "back")
self.navigationItem.leftBarButtonItem = leftItem
let numLbl: UILabel = UILabel ()
numLbl.frame = CGRectMake (10, 100, 300, 40)
numLbl.text = self.num
numLbl.backgroundColor = UIColor.lightGrayColor ()
numLbl.textAlignment = NSTextAlignment.Center
self.view.addSubview (numLbl)
nameTF = UITextField (frame: CGRectMake (10, 150, 300, 35))
nameTF.placeholder = "Enter name"
nameTF.textAlignment = NSTextAlignment.Center
nameTF.borderStyle = UITextBorderStyle.Line
nameTF.clearButtonMode = UITextFieldViewMode.WhileEditing
nameTF.delegate = self
self.view.addSubview (nameTF)
var submitBtn: UIButton = UIButton (frame: CGRectMake (10, 210, 300, 40))
submitBtn.backgroundColor = UIColor.lightGrayColor ()
submitBtn.setTitle ("OK", forState: UIControlState.Normal)
submitBtn.addTarget (self, action: "submitClick:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview (submitBtn)
}
Here is a Cancel button, the other and the same as the home page, the implementation of the click confirmation Code:
func submitClick (sender: UIButton)
{
goBack ()
}
func goBack () {
if nameTF.text.isEmpty {
var alert: UIAlertView = UIAlertView (title: "Cannot be empty", message: "Fill in your name", delegate: nil, cancelButtonTitle: "I know")
alert.show ()
nameTF.becomeFirstResponder ()
} else {
self.dismissViewControllerAnimated (true, completion: {()-> Void in
println ("I'm sure, do you know?");
})
}
}
// MARK: TFDELEGATE
func textFieldShouldReturn (textField: UITextField)-> Bool {
goBack ()
return true
}
override func touchesBegan (touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing (true)
}
Here the code is also noted, more than one click on the view cancel so edit the response to the event
Here the pages can switch into each other, the specific code is really very different, in which the swift syntax and UI syntax to learn by themselves
Third, create an agreement
in the The following code is written in the Homeviewcontroller file:
import UIKit
protocol RegisterDelegate{
func registerName(name : NSString)
}
class HomeViewController: UIViewController,RegisterDelegate {
var nameLbl : UILabel!
var numTF : UITextField!
Note to implement this Protocol, then implement the Protocol method as follows:
Func Registername (name:nsstring) { Namelbl.text = name }
Understand Object-c's friends should understand this step, and before this we have to implement a Protocol object on the entrusted page, the code is as follows:
import UIKit
class RegisterViewController: UIViewController,UITextFieldDelegate{
var nameTF : UITextField!
var delegate : RegisterDelegate!
var num : String!
override func viewDidLoad() {
super.viewDidLoad()
Notice here the delegate, this is the object of the agreement, it is to put the previous home page This object value to give delegate, and delegate followed the agreement, So when the delegate calls the protocol inside the method will be the home page in the home to perform the implementation of this method, so that the value of the registration page to the homepage to show the effect, which is the agreement and the use of the benefits of the delegation, well, if you see here dizzy Head, Please continue to look down, do not take care of this sentence, we go back to home to see how to set up the agent, the code is as follows, by the way I am going to the school to the registration page to go to the code:
func goRegister () {
if numTF.text.isEmpty {
var alert: UIAlertView = UIAlertView (title: "Cannot be empty", message: "Fill in your student ID", delegate: nil, cancelButtonTitle: "I know")
alert.show ()
numTF.becomeFirstResponder ()
} else {
var rootVC: RegisterViewController = RegisterViewController ()
rootVC.delegate = self;
rootVC.num = self.numTF.text
let NVC: UINavigationController = UINavigationController (rootViewController: rootVC)
self.presentViewController (NVC, animated: true, completion: nil)
}
}
Then we go back to the registration page and see where the client has called the Protocol method:
func goBack () {
if nameTF.text.isEmpty {
var alert: UIAlertView = UIAlertView (title: "Cannot be empty", message: "Fill in your name", delegate: nil, cancelButtonTitle: "I know")
alert.show ()
nameTF.becomeFirstResponder ()
} else {
self.delegate! .registerName (self.nameTF.text)
self.dismissViewControllerAnimated (true, completion: {()-> Void in
println ("I'm sure, do you know?");
})
}
}
Note that the difference is not empty here the name passed to the protocol method of the parameters inside the page to display, the implementation of a callback value function.
If you complete the previous steps, you can test if you can pass the value to each other, and if successful, you can rest, Below simply mention appdelegate.swift inside how I write, notice ah, I put the default viewcontroller.swift created after the deletion of AH
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window?.rootViewController = HomeViewController()
return true
}
Fourth Step, replace the Main.storyboard file with pure code, delete the Main.storyboard first
The code is as follows:
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.makeKeyAndVisible()
self.window?.rootViewController = HomeViewController()
return true
}
This is equivalent to pure code to implement all the code
Fifth step, modify the display name
Because Xcode6 does not have the Infoplist.strings file, this you need to create a file with the same name, and because the info file is missing the Key:bundle display name, which you manually add, Then write the same code as XCODE5 in the Infoplist.strings file:
"Cfbundledisplayname" = "Study number registration";
At this point we realized the initial set of all functions, it can be said that this is also the author of a groping, so simple a demo but spent half a day, of which there is no lack of access to grammar, functional search, really hard-won, hoping to bring efficiency to the reader, the following is the realization:
Appendix: Reproduced this blog, please specify the source, the maintenance of copyright, everyone is responsible.
Using Swift's protocol syntax to achieve the value-to-function between pages