注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

云水禅心

淡若秋菊何妨瘦, 清到梅花不畏寒.

 
 
 

日志

 
 

Building Native iOS Apps with Wax(lua): Creating a Sample Application 1  

2011-08-11 12:02:58|  分类: iphone |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
http://mobile.tutsplus.com/tutorials/iphone/iphone-wax-part-2/
Building Native iOS Apps with Wax: Creating a Sample Application
Tutorial Details
  • Technology: iOS SDK + Wax
  • Difficulty: Intermediate
  • Completion Time: 1 Hour (Estimated)
This entry is part 2 of 2 in the series Building Native iOS Apps with Wax

In part one, we learned about what Wax is and what makes it so awesome. Now, in part 2, we are going to step through making a simple application in Wax that displays a list of currently trending topics on Twitter that can be refreshed with a button.

Final Result:需要你的网络能连到http://api.twitter.com/1/trends.json 不然看不到下面结果的  json 的内容:

{"trends":[{"name":"#IfIWereU","url":"http:\/\/twitter.com\/search\/%23IfIWereU"},{"name":"#TengoUnTraumaCon","url":"http:\/\/twitter.com\/search\/%23TengoUnTraumaCon"},{"name":"#ScaryMovieRulesToLiveBy","url":"http:\/\/twitter.com\/search\/%23ScaryMovieRulesToLiveBy"},{"name":"Derek Bieber","url":"http:\/\/twitter.com\/search\/%22Derek%20Bieber%22"},{"name":"Barbara Moncada","url":"http:\/\/twitter.com\/search\/%22Barbara%20Moncada%22"},{"name":"Tyga Montana","url":"http:\/\/twitter.com\/search\/%22Tyga%20Montana%22"},{"name":"Reynaldo Gianecchini","url":"http:\/\/twitter.com\/search\/%22Reynaldo%20Gianecchini%22"},{"name":"Kim Burrell","url":"http:\/\/twitter.com\/search\/%22Kim%20Burrell%22"},{"name":"Lopez Tonight","url":"http:\/\/twitter.com\/search\/%22Lopez%20Tonight%22"},{"name":"Danna Paola","url":"http:\/\/twitter.com\/search\/%22Danna%20Paola%22"}],"as_of":"Thu, 11 Aug 2011 04:26:05 +0000"}

Final result

Step 1 Analyzing AppDelegate.lua

If you look at the current AppDelegate.lua, you should notice a few things. First, you should notice the very first line of the Lua file. This line is the Objective-C class declaration. We create a class called “AppDelegate” that conforms to the “UIApplocationDelegate” protocol. Next, you should see the only method in the file: “applicationDidFinishLaunching”. This famous method is called each time the application is launched and the app can now display things on screen. The code in “applicationDidFinishLaunching” is fairly self-explanatory. It creates a new window with a teal background and when places some white text on. However, we want a Twitter client, not a “Hello Lua!” screen. Let’s start by creating our UITableViewController.

Step 2 Our Old Friend UITableViewController

Make a new file in the script directory called “TwitterViewController.lua”. If you have a good memory, you will remember that to declare a new Objective-C class, we need to use the waxClass function. If you have a  really good memory, you will remember that waxClass works like this:

1
waxClass{"CLASS NAME", "PARENT_CLASS"}

We need a class that extends UITableViewController, so it looks like this:

1
waxClass{"TwitterViewController", UITableViewController}

Now that we have our class defined, let’s handle initialization. In our “init” method, we set the table to be “grouped” instead of “plain”, like it is by default. Since we are also going to be displaying a table of trends, it also makes sense for us to initialize a Lua table to hold all of the trends:

1
2
3
4
5
6
function init(self)
    self.super:initWithStyle(UITableViewStyleGrouped)
    self.trends = {}
 
    return self
end

Pretty self explanatory.

Step 3 Prepping the Table

Now that we have a grouped table, we need a title. We do this in the “viewDidLoad:” method.

1
2
3
4
function viewDidLoad(self)
    self:setTitle("My First Wax Application")
    self:tableView():setAllowsSelection(false)
end

Very self-explanatory. We simply set the title in the bar at the top of the screen and then prevent the user from selecting any table rows. We don’t want the user selecting any rows because we do not intend to handle that action. You can see what other methods you can call on Apple’s documentation page for UITableViewController. Just remember that instead of using properties you have to use the “:property()” and “:setProperty(value)” methods. Press “Run” in the upper-left hand corner of Xcode, and your application should look something like this:

After step 3

Step 4: UITableViewController Data Methods

Our app launches, which is great, but we want to display some data. To display this data there are a few methods that all UITableViewController’s must implement to tell the device what data to display. The first of these methods is “numberOfSectionsInTableView:”, which should return the number of groupings that will be displayed in the table. This is very easy with this app because we will only ever need one section, the section with the current trends:

1
2
3
function numberOfSectionsInTableView(self, tableView)
    return 1
end

Easy, right? Now we have to implement the “tableView_numberOfRowsInSection” method which tells the device how many rows there will be for a specific grouping. This again is very easy for this app because we only have one section. Remember how we initialized a Lua table in the “init” method? We can simply count the number of items in that table and we will know how many rows this table will need to display:

1
2
3
function tableView_numberOfRowsInSection(self, tableView, section)
    return #self.trends
end

This uses the Lua short-hand method of counting the number of items in a table. If you are not familiar with Lua tables, here are a few pointers:

  1. What is called a dictionary in most languages is called a table in Lua.
  2. What is called an array in most languages is called a table with ordered numerical keys.
  3. “Arrays” have 1 based indices, as opposed to 0 based indices in just about every other language on the planet.

Next is the “tableView_titleForHeaderInSection” method. This method tells the device what it should display a the title for a grouping. You simply return a string for the specified grouping and then the title magically appears above the table rows:

1
2
3
4
5
6
7
function tableView_titleForHeaderInSection(self, tableView, section)
    if section == 0 then
        return "Currently Trending Topics"
    end
 
    return nil
end


第二部分 接:http://linglong117.blog.163.com/blog/static/277145472011711113016957/ 

Building Native iOS Apps with Wax(lua): Creating a Sample Application 2  


  评论这张
 
阅读(2125)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018