Skip to content

Instantly share code, notes, and snippets.

@PattyAppier
Created April 23, 2018 17:03
Show Gist options
  • Save PattyAppier/40a882683bf35cc8526e413312407518 to your computer and use it in GitHub Desktop.
Save PattyAppier/40a882683bf35cc8526e413312407518 to your computer and use it in GitHub Desktop.
PattysLucky7MapApp-0423-2018.md
```
// ViewController.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/21.
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
/*@IBAction func geocode(_ sender: UIButton) {
}*/
var lucky7store: Luck7Stores!
override func viewDidLoad() {
super.viewDidLoad()
//0423, to convert location into coordinate
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(lucky7store.location, completionHandler: {
placemarks, error in
if error != nil {
print( "error" ) // shall insert error
return
}
//to retrieve the 1st location
if let placemarks = placemarks { // placemarks 本身是CLPlacmark的屬性
let placemark = placemarks[0]
// to pin its location
let annotation = MKPointAnnotation()
annotation.title = self.lucky7store.name
// to show the pin on map
if let location = placemark.location {
annotation.coordinate = location.coordinate
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
self.mapView.showsUserLocation = true // 帥呆了!人的當下定位, = true自己加的不確定有無錯
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
```
```
// TableViewController.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/21.
import UIKit
import MapKit
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
```
// AppDelegate.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/21.
// Copyright © 2018年 sendbest. All rights reserved.
import UIKit
import MapKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
```
// Lucky7TableViewCell.swift
// This is page of Subclass in MVC.
// PattysMapApp2018
// Created by sendbest on 2018/4/22.
// Copyright © 2018年 sendbest. All rights reserved.
import UIKit
class Lucky7TableViewCell: UITableViewCell {
//0422. to add fieldlable & value lable in cell
@IBOutlet var fieldLabel: UILabel!
@IBOutlet var valueLabel: UILabel!
@IBOutlet var fieldLabelB: UILabel!
@IBOutlet var valueLabelB: UILabel!
@IBOutlet var fieldLabelC: UILabel!
@IBOutlet var valueLabelC: UILabel!
@IBOutlet var fieldLabelD: UILabel!
@IBOutlet var valueLabelD: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
```
// Lucky7Stores.swift
// it is a model to save the array of elements. And it is a instance of the Class Type.
// and then do its var proerties as initialized properties by using self statement and init(){}, including data type of Bool and String||int.
// PattysMapApp2018
// Created by sendbest on 2018/4/23.
// Copyright © 2018年 sendbest. All rights reserved.
import Foundation
class Luck7Stores {
var name = ""
var type = ""
var location = "" // or to declare it as Optional by using: var location: String?
var image = ""
var isVisited = false
var tel = ""
var durationHrs = ""
var isNearby = false
init(name: String, type: String, location: String, image: String, isVisited: Bool, tel: String, durationHrs: String, isNearby: Bool) {
self.name = name
self.type = type
self.location = location
self.image = image
self.isVisited = isVisited
self.tel = tel
self.durationHrs = durationHrs
self.isNearby = isNearby
} // 參數和屬性同名。
// to make an Array OBJC to cover the Data
var lucky7Stores: [Luck7Stores] = [
Luck7Stores(name: "行善路門市", type: " ", location: "No.35, Singshan Rd. Neihu Dist., Taipei City 11469 Taiwan (R.O.C.)", image: "", isVisited: false, tel: "02 8790 299", durationHrs: "7:00–23:00", isNearby: false), //StoreA = [0]
Luck7Stores(name: "行忠路門市", type: " ", location: "No.30, Singjhong Rd. Neihu Dist., Taipei City 11494 Taiwan (R.O.C.)", image: "", isVisited: false, tel: "02 2790 6651", durationHrs: "24 hrs", isNearby: false), //StoreB = [1]
Luck7Stores(name: "行愛路門市", type: " ", location: "No.128, Sing’ai Rd. Neihu Dist., Taipei City 11494 Taiwan (R.O.C.)", image: "", isVisited: false, tel: "02 2791 7850", durationHrs: "24 hrs", isNearby: false),//StoreC = [2]
Luck7Stores(name: "大潤發門市", type: " ", location: "No.128, Sec. 1, Jiouzong Rd. Neihu Dist., Taipei City 11494 Taiwan (R.O.C.)", image: "", isVisited:false,tel: "02 2796 7031", durationHrs: "24 hrs", isNearby: false),//StoreD = [3]
]
}
```
```
// PattysMapViewController.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/21.
// Copyright © 2018年 sendbest. All rights reserved.
import UIKit
import MapKit // 0421 to import framework.
class PattysMapViewController: UIViewController, MKMapViewDelegate {
// 0422, add button name "Find Your Luck 7!" to display action
@IBAction func showLucky7Message() {
let alertController = UIAlertController(title: "Lets Find Your Luck 7 Store!", message: "Plz Click on the Luck 7 Icon.", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title:"Lets Go!",style: UIAlertActionStyle.default, handler: nil))
present(alertController, animated: true, completion: nil)
}
// 0421 to declare the var of mapView and luckySeven.
@IBOutlet var pattysMapView: MKMapView!
/* var luckySeven: ViewController!*/
override func viewDidLoad() {
// pattysMapView.delegate = self // why warning?
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
```
```
// Lucky7StoreControllerA.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/22.
// Copyright © 2018年 sendbest. All rights reserved.
import UIKit
import MapKit
// 0422. to add protocol of 2nd, 3rd sequence. for input model dataSRC to hereby.
class Lucky7StoreControllerA: UIViewController, UITableViewDataSource, UITableViewDelegate {
// 0423. lcky7Store is an Var hereby, lucky7Stores is an Array instant.
// 0422. to add cell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Lucky7TableViewCell
switch indexPath.row {
case 0:
cell.fieldLabel.text = "Store Name"
cell.valueLabel.text = "行善路門市"
case 1:
cell.fieldLabel.text = "Location"
cell.valueLabel.text = "台北市內湖區行善路35號"
case 2:
cell.fieldLabel.text = "Duration"
cell.valueLabel.text = "07:00–23:00"
case 3:
cell.fieldLabel.text = "Tel Number"
cell.valueLabel.text = "02 8790 2990"
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
```
```
// Lucky7StoreControllerB.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/22.
// Copyright © 2018年 sendbest. All rights reserved.
import UIKit
import MapKit
// 0422. to add protocol of 2nd, 3rd sequence. for input model dataSRC to hereby.
class Lucky7StoreControllerB: UIViewController, UITableViewDataSource, UITableViewDelegate {
// 0422. to add cell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Lucky7TableViewCell
switch indexPath.row {
case 0:
cell.fieldLabelB.text = "Store Name"
cell.valueLabelB.text = "行忠路門市"
case 1:
cell.fieldLabelB.text = "Location"
cell.valueLabelB.text = "台北市內湖區行忠路30號"
case 2:
cell.fieldLabelB.text = "Duration"
cell.valueLabelB.text = "24 hrs"
case 3:
cell.fieldLabelB.text = "Tel Number"
cell.valueLabelB.text = "02 2790 6651"
default:
cell.fieldLabelB.text = ""
cell.valueLabelB.text = ""
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
```
```
// Lucky7StoreControllerC.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/22.
// Copyright © 2018年 sendbest. All rights reserved.
import UIKit
import MapKit
// 0422. to add protocol of 2nd, 3rd sequence. for input model dataSRC to hereby.
class Lucky7StoreControllerC: UIViewController, UITableViewDataSource, UITableViewDelegate {
// 0422. to add cell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Lucky7TableViewCell
switch indexPath.row {
case 0:
cell.fieldLabelC.text = "Store Name"
cell.valueLabelC.text = "行愛路門市"
case 1:
cell.fieldLabelC.text = "Location"
cell.valueLabelC.text = "台北市內湖區行愛路128號"
case 2:
cell.fieldLabelC.text = "Duration"
cell.valueLabelC.text = "24 hrs"
case 3:
cell.fieldLabelC.text = "Tel Number"
cell.valueLabelC.text = "02 2791 7850"
default:
cell.fieldLabelC.text = ""
cell.valueLabelC.text = ""
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
```
```
// Lucky7StoreControllerD.swift
// PattysMapApp2018
// Created by sendbest on 2018/4/22.
// Copyright © 2018年 sendbest. All rights reserved.
import UIKit
import MapKit
// 0422. to add protocol of 2nd, 3rd sequence. for input model dataSRC to hereby.class
class Lucky7StoreControllerD: UIViewController, UITableViewDataSource, UITableViewDelegate {
// 0422. to add cell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Lucky7TableViewCell
switch indexPath.row {
case 0:
cell.fieldLabelD.text = "Store Name"
cell.valueLabelD.text = "大潤發門市"
case 1:
cell.fieldLabelD.text = "Location"
cell.valueLabelD.text = "台北市內湖區舊宗路一段128號"
case 2:
cell.fieldLabelD.text = "Duration"
cell.valueLabelD.text = "24 hrs"
case 3:
cell.fieldLabelD.text = "Tel Number"
cell.valueLabelD.text = "02 2796 7031"
default:
cell.fieldLabelD.text = ""
cell.valueLabelD.text = ""
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment