我已经成功地让我的天气应用程序工作了,显示了当前的测试数据等等。但现在唯一的问题是,我只能通过按下我的“刷新”按钮来获取数据。如何使我的应用程序在打开UI时更新它?
这是我的工作代码:
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()
}
}我所看到的是使用AppDelegate.swift文件及其内置的func "func applicationDidEnterBackground(application: UIApplication)“,但是我尝试的每一件事要么破坏了应用程序,要么就是不能工作。我想让应用程序运行getTemp函数,getHumid函数,然后用值更新UI,就像在按钮中的IBAction函数中所做的那样。
任何帮助都是非常感谢的。
编辑:
//
// 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)
}
}试图添加第二个解决方案时,无法使用deinit()运行。
发布于 2015-04-17 23:02:53
让应用程序每次打开时做一些事情的标准技巧是,在应用程序第一次打开时将日期存储在NSUserDefaults中,然后根据应用程序再次打开时的当前日期检查存储的日期。如果时间已经过了足够多的时间,你就会做这个动作,否则你就不会做了。如果你做了这个动作,那么你可以将当前的日期存储回NSUserDefaults中,开始一个新的句号。
发布于 2015-04-17 23:07:32
第一个解决方案:您可以在viewcontroller中使用通知,将其添加到viewDidLoad中
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationWillEnterForegroundNotification, object: nil, queue: NSOperationQueue.mainQueue())
{ [unowned self] notification in
getHumid()
getTemp()
}第二种解决方案:在viewDidLoad中交替使用
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWasOpened:", name: UIApplicationWillEnterForegroundNotification, object: nil)和viewController的一个新函数
func appWasOpened(notification: NSNotification!)
{
getHumid()
getTemp()
}在这两种情况下,()都需要覆盖denit (这将防止在取消分配视图控制器时调用观察)。
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}https://stackoverflow.com/questions/29710642
复制相似问题