Udało mi się sprawić, że moja aplikacja pogodowa działa; wyświetlanie bieżących danych testowych i tak dalej. Ale jedynym problemem jest to, że mogę tylko pobrać aplikację, aby pobrać dane, naciskając przycisk "Odśwież", który zrobiłem. Jak mogę, aby moja aplikacja aktualizowała interfejs użytkownika, gdy jest on otwarty?Automatycznie aktualizuj interfejs użytkownika po otwarciu aplikacji
Oto mój kod roboczych:
import Foundation
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tempLabel: UILabel!
@IBOutlet weak var humidLabel: UILabel!
@IBOutlet weak var refresh: UIButton!
@IBOutlet weak var negitiveSymbol: UILabel!
func getTemp(){
var urlString = "http://torsher.ca/Weather/temperature.txt"
var url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in
var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding))
var tempContent:String = urlContent as String
println(tempContent)
dispatch_async(dispatch_get_main_queue()) {
self.tempLabel.text = (tempContent)
}
}
task.resume()
}
func getHumid(){
var urlString = "http://torsher.ca/Weather/humidity.txt"
var url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in
var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding))
var humidContent:String = urlContent as String
println(humidContent)
dispatch_async(dispatch_get_main_queue()) {
self.humidLabel.text = (humidContent)
}
}
task.resume()
}
@IBAction func refreshPressed(sender: AnyObject, forEvent event: UIEvent) {
getTemp()
}
@IBAction func refreshPress(sender: AnyObject, forEvent event: UIEvent) {
getHumid()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Co Szukałem na mówi użyć pliku AppDelegate.swift i jego wbudowany func „func applicationDidEnterBackground (zastosowanie: UIApplication)”, ale wszystko staram albo łamie aplikację lub po prostu nie działa. Chcę, aby aplikacja uruchamiała funkcję getTemp, getHumid, a następnie aktualizowała interfejs użytkownika z wartościami takimi jak w funkcjach IBAction z przycisków.
Każda pomoc jest bardzo doceniana.
Edycja:
//
// ViewController.swift
// WeatherStation
//
// Created by on 2015-04-16.
// Copyright (c) 2015 . All rights reserved.
//
import Foundation
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tempLabel: UILabel!
@IBOutlet weak var humidLabel: UILabel!
@IBOutlet weak var refresh: UIButton!
@IBOutlet weak var negitiveSymbol: UILabel!
@IBOutlet weak var dateUpdate: UILabel!
func getTemp() //Gets the temeprature from the server and displays it.
{
var urlString = "http://torsher.ca/Weather/temperature.txt"
var url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in
var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding))
var tempContent:String = urlContent as String
println(tempContent)
dispatch_async(dispatch_get_main_queue()) {
self.tempLabel.text = (tempContent)
}
}
task.resume()
}
func getHumid() //Gets the humidity from the server and displays it.
{
var urlString = "http://torsher.ca/Weather/humidity.txt"
var url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithRequest(NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)){(data, response, error) in
var urlContent = (NSString(data: data, encoding: NSUTF8StringEncoding))
var humidContent:String = urlContent as String
println(humidContent)
dispatch_async(dispatch_get_main_queue()) {
self.humidLabel.text = (humidContent)
}
}
task.resume()
}
@IBAction func refreshPressed(sender: AnyObject, forEvent event: UIEvent) //Updates UI with current temperature when refresh is pressed.
{
getTemp()
}
@IBAction func refreshPress(sender: AnyObject, forEvent event: UIEvent) //Updates UI with current humidity when refresh is pressed.
{
getHumid()
}
func appWasOpened(notification: NSNotification!)
{
getHumid()
getTemp()
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(foregroundNotification)
}
override func viewDidLoad()
{
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:
"appWasOpened:", name: UIApplicationWillEnterForegroundNotification, object:
nil)
}
}
Próba dodawanie roztworu drugiego, nie jest w stanie działać z deinit().
Żadne z tych rozwiązań nie działa. Nie jestem pewien, dlaczego, one się kompilują, ale aplikacja nie pobiera danych ani ich nie wyświetla. Jakieś inne pomysły? – MicrosoftDave
one działają na pewno ... musi być coś jeszcze nie tak. powinieneś spróbować umieścić 'println ('some log informtaion')' albo do bloku w pierwszym rozwiązaniu, albo do funkcji 'appWasOpened' w drugim rozwiązaniu, aby sprawdzić, czy są wywoływane. dodatkowo nie można wywołać 'self.appWasOpened (nil)' do metody 'viewWillAppear'. –
Gdzie mogę zastąpić deinit? Próbowałem w AppDelegate i viewController – MicrosoftDave