ios的helloworld 2

参考视频:
www.imooc.com/learn/149
Swift Weather app
使用xcode6_beta
[b]1.create a new xcode project[/b]
single View Application
language选swift
选下面匹配iphone w为compact
如果删掉viewController新建的时候一定要右边第三个
CostumClass 的class选ViewController
[img]http://dl2.iteye.com/upload/attachment/0100/9790/af7e6a38-f886-3f05-9ba8-4197a053f35d.png[/img]
拖拽两个label和一个ImageView
关掉xcode
[b]2. 到项目目录下创建Podfile[/b]

➜ first git:(master) ✗ cat ../hello/Podfile
platform :ios,'7.0'
pod 'AFNetworking'

pod install
如果pod没安装
sudo gem install cocoapods

open .
发现多了一个工程文件
用xcode打开,pod就引用AFNetworking了

[b]3 . supporting files[/b]
下面的info.plist加两个变量
NSLocationAlwaysUsageDescription 我们需要使用你的地理位置
NSLocationUsageDescription 我们需要使用你的地理位置

[b]4. 新建头文件[/b]
[color=red]为了swift调用objective-c的代码[/color]
SwiftWeather-Bridging-Header.h
内容为

#import <AFNetWorking/AFNetWorking.h>

并点项目
build Setting中
搜bridging
Objective-C Bridging Header
设置为
first/SwiftWeather-Bridging-Header.h

[b]5 .ViewController.swift[/b]
内容为

//
// ViewController.swift
// first
//
// Created by xiao7 on 14-9-9.
// Copyright (c) 2014年 killinux. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController ,
CLLocationManagerDelegate{
let locationManager:CLLocationManager = CLLocationManager()
override func viewDidLoad() {
println("aa");
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest

self.loadingIndicatorView.startAnimating()

let background = UIImage(named: "tubiao2.png")
self.view.backgroundColor = UIColor(patternImage:background)

if(ios8()){
locationManager.requestAlwaysAuthorization()
}
locationManager.startUpdatingLocation()

}

@IBOutlet var location : UILabel

@IBOutlet var temperature : UILabel
@IBOutlet var icon : UIImageView
@IBOutlet var loadingIndicatorView : UIActivityIndicatorView
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func ios8() ->Bool {
return UIDevice.currentDevice().systemVersion == "8.0"
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!){
var location:CLLocation = locations[locations.count-1] as CLLocation
if(location.horizontalAccuracy > 0){
println(location.coordinate.latitude)
println(location.coordinate.longitude)
self.updateWeatherInfo(location.coordinate.latitude,longitude:location.coordinate.longitude)
locationManager.stopUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){
println("haoning print error")
println(error)
}
func updateWeatherInfo(latitude:CLLocationDegrees,longitude:CLLocationDegrees){
let manager = AFHTTPRequestOperationManager()
let url = "http://api.openweathermap.org/data/2.5/weather"
//http://api.openweathermap.org/data/2.5/weather?lat=37.8136&lon=144.96&cnt=0
let params = ["lat":latitude,"lon":longitude,"cnt":0]
println("-----")
println(latitude)
manager.GET(url,
parameters:params,
success:{
(operation:AFHTTPRequestOperation!,responseObject: AnyObject!) in println("JSON: "+responseObject.description!)
self.updateUISuccess(responseObject as NSDictionary!)
},
failure:{
(operation:AFHTTPRequestOperation!,error:NSError!) in println("Error:" + error.localizedDescription)
}

)
}
func updateUISuccess(jsonResult:NSDictionary!){
self.loadingIndicatorView.stopAnimating()
self.loadingIndicatorView.hidden = true
if let tempResult = jsonResult["main"]?["temp"]? as? Double{
var thistemperature: Double
if (jsonResult["sys"]?["country"]? as String == "US"){
thistemperature = round(((tempResult - 273.15) * 1.8) + 32)
}else{
thistemperature = round(tempResult - 273.15)
}
println("temperature----->")
println(thistemperature)
self.temperature.text = "\(thistemperature)."
self.temperature.font = UIFont.boldSystemFontOfSize(60)

var name = jsonResult["name"]? as String
self.location.font = UIFont.boldSystemFontOfSize(25)
self.location.text = "\(name)"
println("name----->")
println(name)
var condition = (jsonResult["weather"]? as NSArray)[0]?["id"]? as Int
var sunrise = jsonResult["sys"]?["sunrise"]? as Double
var sunset = jsonResult["sys"]?["sunset"]? as Double

var nightTime = false
var now = NSDate().timeIntervalSince1970
if(now < sunrise || now > sunset){
nightTime = true
}
println("nightTime----->")
println(nightTime)
self.updateWeatherIcon(condition,nightTime: nightTime)
}else{

}
}
func updateWeatherIcon(condition: Int, nightTime: Bool){

if nightTime{
self.icon.image = UIImage(named:"tubiao1")
}else{
self.icon.image = UIImage(named:"tubiao2")
}
}


}





[b]6. 其中@IBOutlet var location : UILabel [/b]
需要
点main.storyboard
按住option 再点ViewController.swift
同时打开两个页面后再
点住control 拖拽 main.storyboard中点标识到ViewController中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值