Swift 应用程序打开时自动更新用户界面
我已经成功地使我的天气应用程序工作;显示当前测试数据等。但现在唯一的问题是,我只能通过按下我制作的“刷新”按钮让应用程序获取数据如何让我的应用程序在打开时更新UI? 这是我的工作代码:Swift 应用程序打开时自动更新用户界面,swift,Swift,我已经成功地使我的天气应用程序工作;显示当前测试数据等。但现在唯一的问题是,我只能通过按下我制作的“刷新”按钮让应用程序获取数据如何让我的应用程序在打开时更新UI? 这是我的工作代码: import Foundation import UIKit class ViewController: UIViewController { @IBOutlet weak var tempLabel: UILabel! @IBOutlet weak var humidLabel: UILabel! @I
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 applicationdenterbackground(application:UIApplication)”,但我尝试的一切要么破坏了应用程序,要么就是不起作用。我想让应用程序运行getTemp函数、get湿润函数,然后用我在iAction函数中使用的按钮值更新UI
非常感谢您的帮助
编辑:
//
// 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()运行 让应用程序在每次打开时都执行某项操作的标准技巧是在应用程序第一次打开时将日期存储在NSUserDefault中,然后在应用程序再次打开时将存储的日期与当前日期进行核对。如果足够的时间过去了,你就去做,否则你就不去做。如果您签了契约,则将当前日期存储回NSUserDefaults以开始新的期间。第一种解决方案:您可以在
视图控制器中使用通知,将其添加到viewDidLoad
:
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationWillEnterForegroundNotification, object: nil, queue: NSOperationQueue.mainQueue())
{ [unowned self] notification in
getHumid()
getTemp()
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWasOpened:", name: UIApplicationWillEnterForegroundNotification, object: nil)
第二种解决方案:或者在viewDidLoad
中:
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationWillEnterForegroundNotification, object: nil, queue: NSOperationQueue.mainQueue())
{ [unowned self] notification in
getHumid()
getTemp()
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWasOpened:", name: UIApplicationWillEnterForegroundNotification, object: nil)
以及viewController
func appWasOpened(notification: NSNotification!)
{
getHumid()
getTemp()
}
在这两种情况下您都需要覆盖denit
(这样可以防止在解除分配viewcontroller时调用观察结果)
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
这两种解决方案似乎都不起作用。我不知道为什么,它们会编译,但应用程序不会提取数据,也不会显示数据。还有其他想法吗?它们确实起作用。一定是出了什么问题。你应该尝试放置一个println('some log information'))
到第一个解决方案中的块或第二个解决方案中的appwasOpen
函数,检查它们是否被调用。此外,您可以调用self.appwasOpen(nil)
到您的视图将出现
方法。我在哪里覆盖Denit?我在AppDelegate中尝试了viewController@MimoBot在viewcontroller中:)@MimoBot请使用最新代码更新您的问题,以便我们可以找出仍然错误的地方:)