IOS Graduate Design Weather forecast App_ios

Source: Internet
Author: User

September mid-June, began to do my graduation design, has been in the tangle to do what, and then think, since it is to do graduation design, then boldly do something you have not touched it. Then look up the data on the Internet to know that the weather forecast needs to use the open API, and to use the kind that is now maintained, but also free (for our student party, this is very good). The core of the weather app is to request data from the weather APIs, the request is typically JSON data (this JSON data has never been contacted before), and then parse the JSON data and display it in the view. Generally speaking, this should involve "network request", "JSON parse" these two big chunks, just to learn new things, and practice practicing, so decided to make a weather forecast app.

First step, Find Weather APIs
all kinds of Baidu, and then found a relatively new site, respectively, is "heart known weather" and "and wind weather." All need to register, after registration will get a key, used for data request. Two sites have paid and free two options, such as figure, after comparison, apart, selected and wind Weather API.

Second Step, Find a Third-party framework for parsing JSON
after a variety of Baidu also found that some of you recommend, such as: Jsonkit, Mantle, afnetworking, Sbjson, mjextension and so on ... Finally refer to a tutorial, or choose the mantle, of course, not to say other bad use, personal preferences just.
The use of mantle is also very simple.
1, a new inherited from the Mtlmodel class, and let him follow the <MTLJSONSerializing> agreement, note here also to #import <MTLModel.h>
2, in the. h file to define your model, such as:

#import <Mantle/Mantle.h>
#import <MTLModel.h>

@interface tscondition:mtlmodel< Mtljsonserializing>

@property (nonatomic, strong) NSString *cityname;  The city name
@property (nonatomic, strong) NSString *nowcond;  Current weather conditions
@property (nonatomic, strong) NSString *nowtmp;   Current temperature
@property (nonatomic, strong) NSString *winddir;  Wind
@property (nonatomic, strong) NSString *WINDSC;   Wind
@property (nonatomic, strong) NSString *date;   Date
@property (nonatomic, strong) NSString *maxtmp;   The highest temperature
@property (nonatomic, strong) NSString *mintmp;   Minimum temperature
@property (nonatomic, strong) NSString *weatherqlty;//air quality


@end

3, implement the class method in the. m file, realize the mapping of JSON data to the model

+ (Nsdictionary *) Jsonkeypathsbypropertykey
{return
 @{
    @ "CityName": @ "basic.city",
    @ "Nowcond": @ " Now.cond.txt ",
    @" nowtmp ": @" now.tmp ",
    @" Winddir ": @" Now.wind.dir ",
    @" WINDSC ": @" Now.wind.sc ", @"
    Date ": @" Basic.update.loc ", @"
    maxtmp ": @" Daily_forecast.tmp.max ",
    @" mintmp ": @" Daily_forecast.tmp.min ",
    @ "weatherqlty": @ "aqi.city.qlty"
    }

4, of course to accept the JSON data to call the following method, and pass the data you want to the dictionary

Create
The response data for the nsdictionary nsdata *jsondata = ...//interface
nsdictionary *jsondict = [nsjsonserialization Jsonobjectwithdata:jsondata options:0 Error:nil];

Use Mtljsonserialization to create Model objects
catprofile *profile = [Mtljsonadapter modelOfClass:CATProfile.class Fromjsondictionary:jsondict Error:null];

After a repetitive exercise, familiar with using mantle to turn JSON data into models, I started to build the app UI.

The third step, set up a weather forecast page (Show weather page)


Here the main lazy loaded a backgroundview as a background image, a tableview and a number of xib files made of the cell composition, I refer to the mobile phone on the weather app did a little landscaping, is to add a tableheaderview, Then add a scrollview on the ScrollView, showing the city and the date on the top, to the effect that it has been suspended at the top, and the following cell slides under it. (later found that in fact, this can not add tableheaderview, the position of the TableView moved down just fine ... )

=====================10.12 Day Editor ==============================

The interface was adjusted to continue ing ...

Fourth Step, Create Uipagecontrol

After the basic set of weather forecast information how to show, it is necessary to think, how to show a number of weather pages? In order to achieve this demand, I thought of using Uipagecontrol
The main use here is the properties and methods

@property (Nonatomic,strong) Nsmutablearray *viewcontrollerarr;      An array of stored views @property (nonatomic,assign) Nsinteger curpage;     Record the current @property (nonatomic,assign) Nsinteger totalpages; Record total number of pages//* * * * page Turn Backward-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerafterviewcontroller: (Uiviewcontroller *) viewcontroller{self.curpage = ((ViewController *)
 viewcontroller). Page;
 UIImage *bgimg = [Self Createimagewithcolor: ((Viewcontroller *) viewcontroller). View.backgroundcolor];
 [Self.navigationController.navigationBar setbackgroundimage:bgimg Forbarmetrics:uibarmetricsdefault];
 Self.pageControl.currentPage = Self.curpage; if (Self.curpage < self.totalpages-1 && self.curpage!= self.totalpages) {return self.viewcontrollerarr[se
 Lf.curpage + 1];
 }else{return nil; }//Flip Forward-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerbeforeviewcontroller: (Uiviewcontroller *) VIEWController{self.curpage = (Viewcontroller *) viewcontroller). Page;
 UIImage *bgimg = [Self Createimagewithcolor: ((Viewcontroller *) viewcontroller). View.backgroundcolor];
 [Self.navigationController.navigationBar setbackgroundimage:bgimg Forbarmetrics:uibarmetricsdefault];
 Self.pageControl.currentPage = Self.curpage;
 if (self.curpage > 0 && self.curpage!= self.totalpages) {return self.viewcontrollerarr[self.curpage-1];
 }else{return nil;

 }
}

Fifth Step, Add city, use Searchbar

To achieve this requirement, I choose to add a button on the Navigationbar, jump to a view with the search bar, to enter the city name, add the weather page function
The difficulty here is that after adding the view, the Uipagecontrol page adds and turns the logic question, after a period of time diligently or solves.

Sixth Step, Add third party sidebar Llslidemenu

I want to use this sidebar to manage the weather page because I'm using an array to store the city name to load the weather page, so my idea is to add a tableview to the sidebar and then display each element of the array, and then experiment with the Tableviewcell editing method, To remove the corresponding cell, delete the corresponding city name and City page.


Do not miss it until it is adjourned.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.