Right click to class folder, new file, choose CoCoa Class, name it second, move cursor to next box to show Next, Create.
Change import to import UIKit to remove red mark.
Copy viewDidLoad command in to class.
override func viewDidLoad() {
super.viewDidLoad()
}
Declare a Label
var la: UILabel!
Locate, set text, set bacground for view.
la = UILabel(frame: CGRect(x: 100, y: 30, width: 280, height: 25))
la.text = "Enter calendar
year"
la.textColor = UIColor.blueColor()
view.backgroundColor = UIColor.whiteColor()
view.addSubview(la)
Remove status bar.
override func prefersStatusBarHidden() -> Bool {
return true
}
Class second now look like this.
Back to ViewController.swift, copy to above last close
bracket.
func next(sender: UIButton){
let vc = second()
self.presentViewController(vc, animated: true, completion: nil)
}
This function open second class, now connect it to button, copy in
to viewDidLoad.
bu2.addTarget(self,
action: #selector(next(_:)),
forControlEvents: UIControlEvents.TouchUpInside)
Run and press next, new screen look like this
Back to class second.swift, declare a textField, we enter
text on it, copy to above override.
var nhap:UITextField!
Locate, set background, request number phonepad, copy in to viewDidLoad.
nhap = UITextField(frame: CGRect(x:130, y: 70, width: 100, height: 40))
nhap.backgroundColor = UIColor.lightGrayColor()
nhap.textAlignment = NSTextAlignment.Center
nhap.placeholder = "Calendar"
nhap.adjustsFontSizeToFitWidth = true
nhap.font=UIFont.systemFontOfSize(14)
nhap.keyboardType = UIKeyboardType.PhonePad
Add to view view.addSubview(nhap)
Run to see result.
If you can’t see keyboard when click to textField, on Xcode
simulator, go to Hardware, Keyboard, only check to first row, use the same
layout as OS X.
Declare 2 Labels, 2 buttons
var la2: UILabel!
var la3: UILabel!
var bu1:UIButton!
var bu2:UIButton!
Locate, name, set text color.
la2 = UILabel(frame: CGRect(x: 120, y: 125, width: 280, height: 25))
la2.text = "Lunar year:"
la2.textColor = UIColor.blueColor()
la3 = UILabel(frame: CGRect(x: 140, y: 155, width: 280, height: 25))
la3.textColor = UIColor.redColor()
bu1 = UIButton(frame: CGRect(x:80, y: 190, width: 100, height: 32))
bu2 = UIButton(frame: CGRect(x:200, y: 190, width: 100, height: 32))
bu1.setTitle("Year change",forState: .Normal)
bu1.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu1.backgroundColor = UIColor.lightGrayColor()
bu2.setTitle("Quit",forState: .Normal)
bu2.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu2.backgroundColor = UIColor.lightGrayColor()
Add to view.
view.addSubview(bu1)
view.addSubview(bu2)
view.addSubview(la2)
view.addSubview(la3)
Run to see result.
Copy function to change calendar year to lunar year in to
above last close bracket.
func lunaryear(year:Int)-> String {
let remain = year % 12
var lunar = "";
if(remain==1){
lunar="Rooster";
}
else if(remain==2){
lunar="Dog";
}
else if(remain==3){
lunar="Pig";
}
else if(remain==4){
lunar="Rat";
}
else if(remain==5){
lunar="Cow";
}
else if(remain==6){
lunar="Tiger";
}
else if(remain==7){
lunar="Rabbit";
}
else if(remain==8){
lunar="Dragon";
}
else if(remain==9){
lunar="Snake";
}
else if(remain==10){
lunar="Horse";
}
else if(remain==11){
lunar="Goat";
}
else {
lunar="Monkey";
}
return lunar;
}
Copy Toast to show notification to user.
func toast(t:String){
let toastLabel = UILabel(frame: CGRectMake(self.view.frame.size.width/2 - 150, 150, 300, 35))
toastLabel.backgroundColor = UIColor.blackColor()
toastLabel.textColor = UIColor.whiteColor()
toastLabel.textAlignment = NSTextAlignment.Center;
self.view.addSubview(toastLabel)
toastLabel.text = t
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
UIView.animateWithDuration(4.0, delay: 0.5, options:
.CurveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: nil)
}
Copy function control 2 buttons.
func nut1(sender: UIButton){
let nam = nhap.text
if(nam == ""||nam == " "){
toast("Please enter the
year")
}
else{
if let myNumber = NSNumberFormatter().numberFromString(nam!) {
let year = myNumber.integerValue
let lunar=lunaryear(year)
la3.text = "Year of " + lunar
}
else{
toast("Data enter is
not valid");
}
}
}
func nut2(sender: UIButton){
self.dismissViewControllerAnimated(true, completion: nil)
}
Function nut1 get string from TextField, check and show
toast if it blank, if contain value cast to number, call function lunaryear,
set result to third Label.
If can’t cast to number, show toast data enter not valid.
Function nut2 quit current screen back to previous screen.
Run to see result, enter some number to change year.
Press Quit to exit.
No comments:
Post a Comment