第十一天:SwiftLoveTweet

 

参考链接:https://github.com/soapyigu/Swift-30-Projects

 

 1 import UIKit
 2 import Social
 3 
 4 enum Gender: Int {
 5     case Male = 0, Female
 6 }
 7 
 8 extension Selector {
 9     static let endEditing = #selector(UIView.endEditing(_:))
10 }
11 
12 extension UIViewController {
13     func showAlert(title: String, message: String, buttonTitle: String) {
14         let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
15         alert.addAction(UIAlertAction.init(title: buttonTitle, style: .default, handler: nil))
16         present(alert, animated: true, completion: nil)
17     }
18 }

 

 1 import Foundation
 2 
 3 struct Tweet {
 4     var gender: Gender
 5     var name: String
 6     var age: Int
 7     var work: String
 8     var salary: String
 9     var isStraight: Bool
10     
11     var interestedGender: String {
12         switch gender {
13         case .Female:
14             return isStraight ? "Men" : "Women"
15         case .Male:
16             return isStraight ? "Women" : "Men"
17         }
18     }
19     
20     var info: String {
21         return "Hi, I am \(name). As a \(age)-year-old \(work) earning \(salary)/year, I am interested in \(interestedGender). Feel free to contact me!"
22     }
23 }

 

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4     
 5     var tweet: Tweet?
 6     
 7     // MARK: Outlets
 8     @IBOutlet weak var salaryLabel: UILabel!
 9     @IBOutlet weak var straightSwitch: UISwitch!
10     @IBOutlet weak var nameTextField: UITextField!
11     @IBOutlet weak var workTextField: UITextField!
12     @IBOutlet weak var birthdayPicker: UIDatePicker!
13     @IBOutlet weak var genderSeg: UISegmentedControl!
14 
15     override func viewDidLoad() {
16         super.viewDidLoad()
17         // Do any additional setup after loading the view, typically from a nib.
18         
19         birthdayPicker.maximumDate = Date()
20         
21         // dismiss keyboard
22         self.view.addGestureRecognizer(UITapGestureRecognizer.init(target: self.view, action: Selector.endEditing))
23         
24         nameTextField.delegate = self
25         workTextField.delegate = self
26     }
27     
28     @IBAction func salarySliderValueDidChange(_ sendr: UISlider) {
29         salaryLabel.text = "$\((Int)(sendr.value))k"
30     }
31     
32     @IBAction func tweetButtonDidTap(_ sender: UIButton) {
33         func getLabelsInfo() -> (name: String?, work: String?, salary: String?) {
34             guard let name = nameTextField.text,
35                 let work = workTextField.text,
36                 let salary = salaryLabel.text
37                 else {
38                     return (nil, nil, nil)
39             }
40             
41             if name.isEmpty || work.isEmpty || salary.isEmpty {
42                 return (nil, nil, nil)
43             }
44             
45             return (name, work, salary)
46         }
47         
48         func getAge() -> Int? {
49             let ageComponents = Calendar.current.dateComponents([.year], from: birthdayPicker.date, to: Date())
50             return ageComponents.year
51         }
52         
53         // get tweet info
54         let labelsInfo = getLabelsInfo()
55         
56         if let name = labelsInfo.name, let work = labelsInfo.work, let salary = labelsInfo.salary, let age = getAge() {
57             tweet = Tweet(gender: Gender(rawValue: genderSeg.selectedSegmentIndex)!, name: name, age: age, work: work, salary: salary, isStraight: straightSwitch.isOn)
58         } else {
59             tweet = nil
60         }
61         
62         switch tweet {
63         case .some(let tweet):
64             showAlert(title: "Love Tweet", message: tweet.info, buttonTitle: "OK")
65         case .none:
66             showAlert(title: "Info miss or invalid", message: "Please fill out correct personal info", buttonTitle: "OK")
67         }
68     }
69     
70     override func didReceiveMemoryWarning() {
71         super.didReceiveMemoryWarning()
72         // Dispose of any resources that can be recreated.
73     }
74 
75 }
76 
77 extension ViewController: UITextFieldDelegate {
78     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
79         textField.resignFirstResponder()
80         return true
81     }
82 }

 

posted @ 2018-03-02 06:48  鳄鱼不怕牙医不怕  阅读(205)  评论(0编辑  收藏  举报