2015-10-12 90 views
0

如何在另一個類的函數中使用字符串值來更新ViewController上的UILabel? 這裏是我的代碼:使用Swift中另一個類的字符串值更新UILabel

視圖控制器:

進口的UIKit

類的ViewController:UIViewController中,dataEnterdDelegate {

@IBOutlet weak var auaTempLabel: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let weather2 = WeatherService2() 
    weather2.getWeatherData("Oranjestad,AW") 
} 

**func userDidEnterInformation(info: NSString) 
{ 
    testLabel!.text = info as String 
}** 

func setLabel2(information: String) 
{ 
    auaTempLabel.text = information 
} 

命名WeatherService2包含下列代碼的其他類:

**protocol dataEnterdDelegate{ 
func userDidEnterInformation(info:NSString) 
}** 

Class WeatherService2{ 
var currentTempeture:String? 
let targetVC = ViewController() 
**var delegate: dataEnterdDelegate?** 

func getWeatherData(urlString:String) 
{ 
    let url = NSURL(string: urlString)! 
    let sqlQuery = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"\(url)\")" 

    let endpoint = "https://query.yahooapis.com/v1/public/yql?q=\(sqlQuery)&format=json" 

    let testString = (String(endpoint)) 
    getData(testString) 
} 

func getData(request_data: String) 
{ 
    let requestString:NSString = request_data.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! 

    let url_with_data = NSURL(string: requestString as String)! 
    let task = NSURLSession.sharedSession().dataTaskWithURL(url_with_data){ 
     (data, response, error) in dispatch_async(dispatch_get_main_queue(), { 
      if data == nil 
      { 
       print("Failed loading HTTP link") 

      }else{ 
       self.setLabel(data!) 
      } 
     }) 
    } 
    task.resume() 



} 

func setLabel(weatherData:NSData) 
{ 
    enum JSONErrors: ErrorType 
    { 
     case UserError 
     case jsonError 
    } 

    do{ 
     let jsonResults = try NSJSONSerialization.JSONObjectWithData(weatherData, options: .AllowFragments) 

     if let city = jsonResults["query"] as? NSDictionary 
     { 
      if let name = city["results"] as? NSDictionary 
      { 
       if let channel = name["channel"] as? NSDictionary 
       { 
        if let item = channel["item"] as? NSDictionary 
        { 
         if let condition = item["condition"] as? NSDictionary 
         { 
          if let temp = condition["temp"] as? String 
          { 
           setTemp(temp) 
           **delegate!.userDidEnterInformation(temp)** 
          } 
         } 
        } 
       } 

      } 
     } 
    } 

    catch { 
     print("Failed to load JSON Object") 

    } 


} 


func setTemp(tempeture:String) 
{ 
    self.currentTempeture = tempeture 
} 

func getTemp() ->String 
{ 
    return self.currentTempeture! 
} 

}

代碼運行正常,但是當我嘗試更新ViewController中的UILabel時,出現「致命錯誤:意外地發現零,同時展開可選值」錯誤。 當我在視圖控制器類中使用print("The return value is: "+information)時,它會正確輸出返回值。 這就是我現在感到困惑的原因,因爲我不知道爲什麼當試圖使用這個值來更新我的UILabel時,我仍然收到「致命錯誤:意外地發現零,同時展開可選值」。

任何人都可以幫我解決這個問題嗎?

在此先感謝

回答

0

我管理通過執行以下操作來解決這個問題: 我創建了以下類 - 物品 - 條件 - 海峽 這些類實現JSONPopulator協議。 的JSONPopulator協議:

protocol JSONPopulator 
{ 
    func populate(data:AnyObject) 
} 

項目類:

class Item: JSONPopulator 
{ 
var condition:Condition? 

func getCondition() ->Condition 
{ 
    return condition! 
} 

func populate(data: AnyObject) 
{ 
    condition = Condition() 
    condition?.populate(data) 
} 
} 

條件類:

class Condition:JSONPopulator 
{ 

var arubaTemp:String? 

var channel:NSDictionary! 

func getArubaTemp()->String 
{ 
    return arubaTemp! 
} 

func getBonaireTemp() ->String 
{ 
    return bonaireTemp! 
} 

func getCuracaoTemp()->String 
{ 
    return curacaoTemp! 
} 

func populate(data: AnyObject) 
{ 
    if let query = data["query"] as? NSDictionary 
    { 
     if let results = query["results"] as? NSDictionary 
     { 
      if let channel = results["channel"] as? NSDictionary 
      { 
       self.channel = channel 
       if let location = channel["location"] as? NSDictionary 
       { 
        if let city = location["city"] as? String 
        { 
         if city.containsString("Oranjestad") 
         { 
          switch city 
          { 
          case "Oranjestad": 
           arubaTemp = getTemp() 
           print(arubaTemp) 
          default: 
           break 
          } 

         } 
        } 
       } 
      } 
     } 
    } 
} 



func getTemp() ->String 
{ 
    var temp:String? 
    if let item = self.channel["item"] as? NSDictionary 
    { 
     if let condition = item["condition"] as? NSDictionary 
     { 
      if let tempeture = condition["temp"] as? String 
      { 
       print(tempeture) 
       temp = tempeture 
      } 
     } 
    } 
    print(temp) 
    return temp! 
} 

} 

Channel類:

class Channel: JSONPopulator 
{ 
    var item:Item? 
    var unit:Unit? 
    var request_city:String? 

    func setRequestCity(request_city:String) 
    { 
     self.request_city = request_city 
    } 

    func getRequestCity() ->String 
    { 
     return request_city! 
    } 

    func getItem() -> Item 
    { 
     return item! 
    } 

    func getUnit() -> Unit 
    { 
     return unit! 
    } 

    func populate(data: AnyObject) 
    { 
     item = Item() 
     item?.populate(data)  
    } 

} 

The WeatherService處理解析JSON對象的函數的類。這個類實現了一個WeatherServiceCallBack協議。 的WeatherServiceCallBack協議:

protocol WeatherServiceCallBack 
{ 
    func arubaWeatherServiceService(channel:Channel) 
    func arubaWeatherServiceFailure() 
} 

的WeatherService類:

class WeatherService 
{ 
    var weatherServiceCallBack:WeatherServiceCallBack 
    var requestCity:String? 

    init(weatherServiceCallBack: WeatherServiceCallBack) 
{ 
    self.weatherServiceCallBack = weatherServiceCallBack 
} 

internal func checkCity(city:String) 
{ 
    switch (city) 
    { 
     case "Oranjestad,AW": 
     requestCity = city 
     getWeatherData(requestCity!) 
    default: 
     break 
    } 

} 


func getWeatherData(urlString:String) 
{ 
    let url = NSURL(string: urlString)! 

    let sqlQuery = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"\(url)\")" 

    let endpoint = "https://query.yahooapis.com/v1/public/yql?q=\(sqlQuery)&format=json" 


    let testString = (String(endpoint) 
    executeTask(testString) 
} 

func executeTask(request_data: String) 
{ 
    let requestString:NSString = request_data.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! 

    let url_with_data = NSURL(string: requestString as String)! 

    let task = NSURLSession.sharedSession().dataTaskWithURL(url_with_data){ 
     (data, response, error) in dispatch_async(dispatch_get_main_queue(), { 
      if data == nil 
      { 
       print("Failed loading HTTP link") 

      }else{ 
       self.onPost(data!) 
      } 
     }) 
    } 
    task.resume() 

} 

func onPost(data:NSData) 
{ 
    enum JSONErrors: ErrorType 
    { 
     case UserError 
     case jsonError 
    } 

    do{ 
     let jsonResults = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) 
     print(jsonResults) 

     if let city = jsonResults["query"] as? NSDictionary 
     { 
      if let name = city["count"] as? Int 
      { 
       if name == 0 
       { 
        weatherServiceCallBack.arubaWeatherServiceFailure() 
       } 
      } 

     } 

     if let requestCity_check = jsonResults["query"] as? NSDictionary 
     { 
      if let results = requestCity_check["results"] as? NSDictionary 
      { 
       if let channel = results["channel"] as? NSDictionary 
       { 
        if let location = channel["location"] as? NSDictionary 
        { 
         if let city = location["city"] as? String 
         { 
          requestCity = city 
          let channel = Channel() 
          channel.setRequestCity(requestCity!) 
          channel.populate(jsonResults) 
          weatherServiceCallBack.arubaWeatherServiceService(channel) 
         } 

        } 

       } 

      } 
     } 


    }catch { 
     print("Failed to load JSON Object") 
    } 

} 

} 

的ViewController類(I添加一些動畫到的UILabel所以它可以翻轉從華氏攝氏度):

class ViewController: UIViewController, WeatherServiceCallBack 
{ 
    var weather:WeatherService? 
    var aua_Tempeture_in_F:String? 
    var aua_Tempeture_in_C:String? 
    var timer = NSTimer() 
    @IBOutlet var aua_Temp_Label: UILabel! 
    let animationDuration: NSTimeInterval = 0.35 
    let switchingInterval: NSTimeInterval = 5 //10 


    override func viewDidLoad() { 
    super.viewDidLoad() 
    weather = WeatherService(weatherServiceCallBack: self) 
    weather?.checkCity("Oranjestad,AW") 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

    func animateTemptext() 
    { 

self.timer = NSTimer.scheduledTimerWithTimeInterval(7.0, target: self, selector: Selector("tempConvertionTextSwitch"), userInfo: nil, repeats: true) 


    } 

func setTempinCelsius(temp_string:String) 
{ 

    aua_Tempeture_in_F = "\(temp_string)°F" 
    let convertedString = convertFahrenheittoCelsius(temp_string) 
    aua_Tempeture_in_C = "\(convertedString)°C" 
    aua_Temp_Label.text = aua_Tempeture_in_C 
    animateTemptext() 
} 

func convertFahrenheittoCelsius(currentTemp:String) ->String 
{ 
    let tempTocelsius = (String(((Int(currentTemp)! - 32) * 5)/9)) 
    return tempTocelsius 
} 

@objc func tempConvertionTextSwitch() 
{ 
    CATransaction.begin() 
    CATransaction.setAnimationDuration(animationDuration) 
    CATransaction.setCompletionBlock{ 
     let delay = dispatch_time(DISPATCH_TIME_NOW,Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC))) 
     dispatch_after(delay, dispatch_get_main_queue()) 
      { 

     } 

    } 
    let transition = CATransition() 
    transition.type = kCATransitionFade 
    if aua_Temp_Label.text == aua_Tempeture_in_F 
    { 

     aua_Temp_Label.text = aua_Tempeture_in_C 
    }else if aua_Temp_Label.text == aua_Tempeture_in_C 
    { 

     aua_Temp_Label.text = aua_Tempeture_in_F 
    }else if aua_Temp_Label == "" 
    { 
     aua_Temp_Label.text = aua_Tempeture_in_C 
    } 
    aua_Temp_Label.layer.addAnimation(transition, forKey: kCATransition) 
    CATransaction.commit() 

} 



func arubaWeatherServiceFailure() { 

} 

func arubaWeatherServiceService(channel: Channel) 
{ 
    let requested_city = channel.getRequestCity() 
    let items = channel.getItem() 
    let aua_Temp = items.getCondition().getArubaTemp() 
    setTempinCelsius(aua_Temp) 

} 

} 

參考:

  • 的iOS 8斯威夫特編程食譜解決方案對於iOS應用

  • 的iOS 8編程基礎雨燕雨燕時,Xcode和可可基礎

例子

希望它幫助曾經有過同樣問題的人

0

爲此,您必須創建委託方法。

viewController創建委託方法,並從那裏你會得到響應調用它並設置viewController.delegate = self

我無法解釋你越是要搜索的是,它會工作的100%。

一切順利。

+0

你的意思是在類Viewcontroller中創建一個類型爲String的變量嗎? – Melchior

+0

是的,你的插座是在哪一類中定義的。 –

+0

好吧,因爲我已經做到了,但仍然沒有積極的迴應。如果你可以發表一個關於如何完成的例子,我不是嗎? – Melchior

相關問題