从另一个类的IBOutlet中获取值

时间:2020-12-05 15:58:00

I'm trying to make a call to my database where the city of my user will be equal to the location found thanks to Core Location. I can successfully make a call to the database and find the geolocation, I can also make a query where city = "nameOfACity" but I don't figure out how to call data where city = cityFromCoreLocation.

我想打电话给我的数据库,在那里我的用户的城市将等于由于核心位置找到的位置。我可以成功地调用数据库并找到地理位置,我还可以查询城市= "nameOfACity",但我不知道如何调用city = cityFromCoreLocation的数据。

I can't call the variable I created for the result of the Geolocation as it's located in another class and displayed as an IBOutlet. I was wondering if I may : - find a way to call that IBOutlet - or call directly the placemark.locality from Geocoder function? (which I can't do for the moment as it is also in another class.

我不能调用为Geolocation结果创建的变量,因为它位于另一个类中,并显示为IBOutlet。我想知道我是否可以:——找到一种方法来调用IBOutlet——或者直接调用placemark。位置从Geocoder函数?(现在我还不能做,因为这也是在另一节课上。

I'm new to swift so I still don't know how to call a variable from a different class... I read topics on this subject but they don't adapt to the current case.

我是swift新手,所以我仍然不知道如何调用来自不同类的变量……我读过这方面的题目,但不适应目前的情况。

Here is my code :

这是我的代码:

Call to the data base :

调用数据库:

import UIKit

let sharedInstance = ModelBD()

class ModelBD: NSObject {

var database: FMDatabase? = nil

class func getInstance() -> ModelBD {
    if (sharedInstance.database == nil) {
        sharedInstance.database = FMDatabase(path: Utilities.getPath("bddFrance.sqlite"))
    }

    return sharedInstance
}


func getAllArticles() -> NSMutableArray {
    sharedInstance.database!.open()

    let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM bddF WHERE ville = \(VC0.myCity)", withArgumentsInArray: nil)
    let arrDataArticles : NSMutableArray = NSMutableArray()

    if (resultSet != nil) {
        while resultSet.next() {
            let articlesInfo : ArticlesData = ArticlesData()
            articlesInfo.nameArticle = resultSet.stringForColumn("nom")
            articlesInfo.cityArticle = resultSet.stringForColumn("ville")
            articlesInfo.districtArticle = resultSet.stringForColumn("quartier")
            articlesInfo.countryArticle = resultSet.stringForColumn("pays")

            print("--")
            print("nameArticle \(articlesInfo.nameArticle)")
            print("cityArticle \(articlesInfo.cityArticle)")
            print("districtArticle \(articlesInfo.districtArticle)")
            print("countryArticle \(articlesInfo.countryArticle)")

            arrDataArticles.addObject(articlesInfo)
        }
    }

    sharedInstance.database!.close()
    return arrDataArticles
}
}

Class where is located the variable that I need :

我需要的变量所在的类:

import UIKit
import CoreLocation

class VC0: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {

let LocationManager = CLLocationManager()

var arrDataArticles: NSMutableArray!

@IBOutlet weak var myCity: UINavigationItem!

@IBOutlet weak var myTableView: UITableView!

var images = UIImage(named: "avatar")

override func viewDidLoad() {
    super.viewDidLoad()

    //CoreLocation
    self.LocationManager.delegate = self
    self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.LocationManager.requestWhenInUseAuthorization()
    self.LocationManager.startUpdatingLocation()

    //Data
    self.getAllArticles()
}

//Location
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
        (placemarks, error) -> Void in

        if error != nil {
            print("Error")
        }

        if let pm = placemarks?.first {
            self.displayLocationInfo(pm)
        }
        else {
            print("Error : data error")
        }

    })
}

func displayLocationInfo (placemark: CLPlacemark) {

    self.LocationManager.stopUpdatingLocation()

    print(placemark.subThoroughfare)
    print(placemark.thoroughfare)
    print(placemark.locality)
    print(placemark.postalCode)
    print(placemark.subAdministrativeArea)
    print(placemark.administrativeArea)
    print(placemark.country)
    myCity.title = placemark.locality

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error :" + error.localizedDescription)
}

//Data
func getAllArticles() {
    arrDataArticles = NSMutableArray()
    arrDataArticles = ModelBD.getInstance().getAllArticles()
    myTableView.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrDataArticles.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let myCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell

    let article: ArticlesData = arrDataArticles.objectAtIndex(indexPath.row) as! ArticlesData

    myCell.rank.text = "\(indexPath.row + 1)"
    myCell.photo.image = images
    myCell.name.text = article.nameArticle
    myCell.city.text = article.districtArticle

    return myCell
}

}

Thanks in advance for your help.

谢谢你的帮助。

1 个解决方案

#1


1  

Modify your getAllArticles method in ModelBD class to accept city (String) as an argument.

在ModelBD类中修改getAllArticles方法,以接受city (String)作为参数。

func getAllArticlesFromCity(city: String) -> NSMutableArray {
    sharedInstance.database!.open()

    let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM bddF WHERE ville = '\(city)'", withArgumentsInArray: nil)
    ...

Then in your VC0, you can just pass the city in that method:

然后在VC0中,你可以通过这个方法通过城市:

func getAllArticles() {
    if let city = myCity.title {
      arrDataArticles = NSMutableArray()
      arrDataArticles = ModelBD.getInstance().getAllArticlesFromCity(city)
      myTableView.reloadData()
    } 
}

And you should only call self.getAllArticles after you get the location. Don't call it on viewDidLoad.

你应该只打电话给self。获取位置后的getAllArticles。不要在viewDidLoad调用它。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  //...
  if let pm = placemarks?.first {
    self.displayLocationInfo(pm)
    self.getAllArticles()
  }
  //...

#1


1  

Modify your getAllArticles method in ModelBD class to accept city (String) as an argument.

在ModelBD类中修改getAllArticles方法,以接受city (String)作为参数。

func getAllArticlesFromCity(city: String) -> NSMutableArray {
    sharedInstance.database!.open()

    let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM bddF WHERE ville = '\(city)'", withArgumentsInArray: nil)
    ...

Then in your VC0, you can just pass the city in that method:

然后在VC0中,你可以通过这个方法通过城市:

func getAllArticles() {
    if let city = myCity.title {
      arrDataArticles = NSMutableArray()
      arrDataArticles = ModelBD.getInstance().getAllArticlesFromCity(city)
      myTableView.reloadData()
    } 
}

And you should only call self.getAllArticles after you get the location. Don't call it on viewDidLoad.

你应该只打电话给self。获取位置后的getAllArticles。不要在viewDidLoad调用它。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  //...
  if let pm = placemarks?.first {
    self.displayLocationInfo(pm)
    self.getAllArticles()
  }
  //...