Skip to content

Instantly share code, notes, and snippets.

View sunnyleeyun's full-sized avatar
🎯
Focusing

Sunny sunnyleeyun

🎯
Focusing
View GitHub Profile
class UITextViewPadding : UITextView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textContainerInset = UIEdgeInsets(top: 8, left: 4, bottom: 8, right: 4)
}
}
import Foundation
class UITextFieldPadding : UITextField {
let padding = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
import Foundation
import UIKit
@IBDesignable
class DesignableView: UIView {
}
@IBDesignable
class DesignableButton: UIButton {
}
// Temporary view
self.question_View.addSubview(temp_View)
temp_View.backgroundColor = UIColor.white
temp_View.snp.makeConstraints { (make) in
make.edges.equalTo(question_View)
}
// Topic Label
temp_View.addSubview(topic_Label)
topic_Label.text = question.title
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController) {
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if (rootViewController.responds(to: Selector(("canRotate")))) {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown;
}
}
// Only allow portrait (standard behaviour)
return .portrait;
//c. 處理TableviewCell的DishesTableViewCell。
class DishesTableViewCell: UITableViewCell {
@IBOutlet weak var nameMeal: UILabel!
@IBOutlet weak var timeMeal: UILabel!
@IBOutlet weak var imageMeal: UIImageView!
//b. 創造一個Swift檔(Meals),裝載NSObject,這裡的名稱都要跟Firebase裡面的Child名稱一模一樣。
class Meals: NSObject{
var FoodName: String?
var cookTime: String?
var cookPic: String?
var cookhow: String?
// a.原本的ViewController(MainTableViewController) 部份截取,詳細請至github下載完整專案
func fetchMealsList(){ //將這個function放至viewDidLoad中
       
//之前提到的.childAdded是專門處理List
      refHandle = databaseRef.child("Meal").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String : AnyObject]{
let mealDetail = Meals()
//確定按鈕 Confirm (儲存資料並到Tableview頁面)
@IBAction func confirm(_ sender: Any) {
// 這些空格不能是空的
if cookName.text != "" && foodName.text != "" && cookTime.text != "" && cookHow.text != ""{
// Meal > uniqueString > NSObject > value
//之後在MainTableViewController會看到 *.childAdded
// 通常為了處理 list 的資料,像是許多聊天室的練習就會用到 childAdded ,因為聊天內容都是非常多句,也可以稱為一個list