Skip to content

Instantly share code, notes, and snippets.

View KrauserHuang's full-sized avatar
:bowtie:
Learning iOS right now!

Krauser Huang KrauserHuang

:bowtie:
Learning iOS right now!
  • Taiwan
View GitHub Profile
import Foundation
import UIKit
protocol BotanicalModelProtocol {
func botanicalResultsRetrieved(_ results: [ResultsDetail])
}
enum BotanicalError: Error {
case invalidURL
case noDataAvailable
class ShadowView: UIView {
override var bounds: CGRect {
didSet {
setupShadow()
}
}
private func setupShadow() {
self.layer.cornerRadius = 10
self.layer.shadowOffset = CGSize(width: 0, height: 3)
borderView.layer.cornerRadius = 10
borderView.layer.masksToBounds = true
shadowView.backgroundColor = .clear //與cornerRadius擇一
shadowView.layer.cornerRadius = 10 //與backgroundColor擇一
shadowView.layer.masksToBounds = false
shadowView.layer.shadowColor = UIColor.black.cgColor
shadowView.layer.shadowRadius = 4
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.shadowOffset = CGSize(width: 2, height: 2)
func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWasShown(_ notification: Notification) {
guard let info = notification.userInfo,
let keyboardFrameValue = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = keyboardFrameValue.cgRectValue
let keyboardSize = keyboardFrame.size
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
self.view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
self.view.endEditing(true)
}
extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// remove the textField from first responder
textField.resignFirstResponder()
return true
}
}
@KrauserHuang
KrauserHuang / oneTabVCToAnotherTabVC.swift
Created February 24, 2022 13:23
Pass data from one view controller to another one which both connect tabbar controller and navigation controller
// self == TopPageNavigationController
// use .selectedIndex to jump to second tabbar(means to ShopListViewController)
self.tabBarController?.selectedIndex = 1
// use .selectedViewController to match your index, because ShopListViewController is tied with navigation controller
// you need to down cast to the right navigation controller in order to find the controller you want(ShopListViewController)
let shopNavigationController = self.tabBarController?.selectedViewController as? ShopNavigationController
// to remove all the related view controllers from ShopNavigationController except itself
shopNavigationController?.popToRootViewController(animated: false)
if let shopListViewController = shopNavigationController?.viewControllers.compactMap({ $0 as? ShopListViewController }).first {
// do property to change, and here you can pass data
// 抓取訂單資料
func fetchData() {
let urlStr = "https://sheetdb.io/api/v1/ocn6qrzzdg1mn"
let url = URL(string: urlStr)
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let data = data {
let decoder = JSONDecoder()
// 將資料上傳到google sheet上面
func postOrder() {
orderPreference()
let orderItem = OrderInfo(orderer: nameTextField.text!,
drinkName: order.drinkName!,
sizeLevel: order.sizeLevel!.rawValue,
iceLevel: order.iceLevel!.rawValue,
sugarLevel: order.sugarLevel!.rawValue,
extraToppings: order.extraToppings!.rawValue,
totalPrice: "\(order.totalPrice!)")
struct PostOrder: Codable {
let data: OrderInfo
}
struct OrderInfo: Codable {
let orderer: String
let drinkName: String
let sizeLevel: String
let iceLevel: String
let sugarLevel: String