<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhoneGeek 爱疯极客 &#187; 用户界面</title>
	<atom:link href="http://www.iphone-geek.cn/tag/%e7%94%a8%e6%88%b7%e7%95%8c%e9%9d%a2/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iphone-geek.cn</link>
	<description>iPhone 新闻，编程，技巧与提示，代码，教程</description>
	<lastBuildDate>Sun, 25 Jul 2010 13:49:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>iPad 编程教程 &#8211; Hello World++</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/ipad-%e7%bc%96%e7%a8%8b%e6%95%99%e7%a8%8b-hello-world</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/ipad-%e7%bc%96%e7%a8%8b%e6%95%99%e7%a8%8b-hello-world#comments</comments>
		<pubDate>Wed, 07 Apr 2010 05:07:44 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=923</guid>
		<description><![CDATA[概述

随着iPad的发布，我想很多人开始绞尽脑汁试图在新一轮的“淘金热”中抢占一块市场。iCodeBlog将推出一系列iPad教程帮助大家。

由于iPad使用iPhone同样的SDK，所有代码看上去完全一样。实际上，看看最新和修改了的API类，你就会发现大部分是有关用户界面的。这是一个好消息，因为我们都对iPhone编程有了一定的经验。

此教程被称为“Hello World”，实际上它远远不止这些。首先，我假定你们已经具有iPhone/Objective-C的编程经验。


我们要做什么

今天的教程，我将展示怎样创建一个iPad项目，使用UISplitViewController显示两个单独的窗格。我还将涉及一些新的设计/UI模式并介绍iPad编程。

此项目将基于使用UITableView显示水果列表教程 。我们将扩展其中一个范例，如下所示：



本文将使用UISplitViewController在左窗格显示一个UITableView，而在右窗格显示一个具有UIImageView子视窗的UIView。实际上创建这样一个项目非常简单，因为项目模板提供了大部分初始代码。
开始


从http://developer.apple.com/iphone/下载SDK 3.2。iPad模拟器包括在了SDK中。 
下载本项目所需的资源文件 iPadHelloWorldResources.zip 。 

创建项目

创建iPad项目与iPhone项目没有分别。打开XCode并选择File-&#62;New Project，你将发现有一个新的Split View-Based Application选项。选择它并命名为iPadHelloWorld。

这将创建一个UITableView位于左窗格，UIView位于右窗格的应用程序。它还甚至提供了一些示例数据。下列文件将被加入到项目中。



各文件的简单介绍如下：


iPadHelloWorldAppDelegate – 类似于 app delegate。在 application:didFinishLaunchingWithOptions 方法中，创建了具有MasterViewController和 DetailViewController的UISplitViewController。 
MasterViewController – UITableViewController。它将处理左方视窗。 
DetailViewController – 它将处理右方视窗。当用户选择左方列表中不同行时，我们将在此视窗中做相应更新。 


按“Build and Run”测试一下。

注意：当你运行此程序时，你只能看到主视窗，这是因为模拟器运行于垂直模式。选取 “Hardware -&#62; Rotate Left/Right”将使模拟器旋转。你还可以通过按下键盘上的CMD-&#62;Arrow Left/Right 键获得同样效果。

 

导入项目图像

下一步你需要导入项目需要的图像。将下载的资源文件解压，并拖入称为“Resources-iPad”的文件夹下。
XCode 将提示你复制这些文件，选择“Yes”并按下OK。



请包括四个图像文件以及fruits.plist。
显示水果列表

显示水果列表与显示其他UITableView中的数据并无区别。打开MasterViewController.h 加入水果数组定义。
#import   

@class DetailViewController;   

@interface MasterViewController : UITableViewController {
    DetailViewController *detailViewController;
    NSArray * fruits;
} 

@property [...]]]></description>
			<content:encoded><![CDATA[<h3>概述</h3>
<p><br class="spacer_" /></p>
<p>随着iPad的发布，我想很多人开始绞尽脑汁试图在新一轮的“淘金热”中抢占一块市场。iCodeBlog将推出一系列iPad教程帮助大家。</p>
<p><br class="spacer_" /></p>
<p>由于iPad使用iPhone同样的SDK，所有代码看上去完全一样。实际上，看看<a href="http://developer.apple.com/iphone/prerelease/library/releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS3_2.html#//apple_ref/doc/uid/TP40009337-SW1">最新和修改了的API类</a>，你就会发现大部分是有关用户界面的。这是一个好消息，因为我们都对iPhone编程有了一定的经验。</p>
<p><br class="spacer_" /></p>
<p>此教程被称为“Hello World”，实际上它远远不止这些。首先，我假定你们已经具有iPhone/Objective-C的编程经验。</p>
<p><br class="spacer_" /></p>
<p><span id="more-923"></span></p>
<h3>我们要做什么</h3>
<p><br class="spacer_" /></p>
<p>今天的教程，我将展示怎样创建一个iPad项目，使用UISplitViewController显示两个单独的窗格。我还将涉及一些新的设计/UI模式并介绍iPad编程。</p>
<p><br class="spacer_" /></p>
<p>此项目将基于<a href="http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/">使用UITableView显示水果列表教程</a> 。我们将扩展其中一个范例，如下所示：</p>
<p><br class="spacer_" /></p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/watermelon-ss.png"><img title="watermelon-ss" src="http://icodeblog.com/wp-content/uploads/2010/02/watermelon-ss.png" alt="" width="500" height="387" /></a></p>
<p><br class="spacer_" /></p>
<p>本文将使用UISplitViewController在左窗格显示一个UITableView，而在右窗格显示一个具有UIImageView子视窗的UIView。实际上创建这样一个项目非常简单，因为项目模板提供了大部分初始代码。</p>
<h3>开始</h3>
<p><br class="spacer_" /></p>
<ol>
<li>从<a href="http://developer.apple.com/iphone/">http://developer.apple.com/iphone/</a>下载SDK 3.2。iPad模拟器包括在了SDK中。 </li>
<li>下载本项目所需的资源文件 <a href="http://icodeblog.com/wp-content/uploads/2010/02/iPadHelloWorldResources.zip">iPadHelloWorldResources.zip</a> 。 </li>
</ol>
<h3>创建项目</h3>
<p><br class="spacer_" /></p>
<p>创建iPad项目与iPhone项目没有分别。打开XCode并选择File-&gt;New Project，你将发现有一个新的Split View-Based Application选项。选择它并命名为iPadHelloWorld。</p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/Screen-shot-2010-02-01-at-12.37.12-PM.png"><img title="Screen shot 2010-02-01  at 12.37.12 PM" src="http://icodeblog.com/wp-content/uploads/2010/02/Screen-shot-2010-02-01-at-12.37.12-PM.png" alt="" width="544" height="418" /></a></p>
<p>这将创建一个UITableView位于左窗格，UIView位于右窗格的应用程序。它还甚至提供了一些示例数据。下列文件将被加入到项目中。</p>
<p><br class="spacer_" /></p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.02.17-AM.png"><img title="Screen shot 2010-02-02  at 11.02.17 AM" src="http://icodeblog.com/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.02.17-AM.png" alt="" width="247" height="349" /></a></p>
<p><br class="spacer_" /></p>
<p>各文件的简单介绍如下：</p>
<p><br class="spacer_" /></p>
<ul>
<li>iPadHelloWorldAppDelegate – 类似于 app delegate。在 application:didFinishLaunchingWithOptions 方法中，创建了具有MasterViewController和 DetailViewController的UISplitViewController。 </li>
<li>MasterViewController – UITableViewController。它将处理左方视窗。 </li>
<li>DetailViewController – 它将处理右方视窗。当用户选择左方列表中不同行时，我们将在此视窗中做相应更新。 </li>
</ul>
<p><br class="spacer_" /></p>
<p>按“Build and Run”测试一下。</p>
<p><br class="spacer_" /></p>
<blockquote><p><strong>注意：当你运行此程序时，你只能看到主视窗，这是因为模拟器运行于垂直模式。选取 “Hardware -&gt; Rotate Left/Right”将使模拟器旋转。你还可以通过按下键盘上的CMD-&gt;Arrow Left/Right 键获得同样效果。</strong></p>
<p><strong><br />
 </strong></p>
</blockquote>
<h3>导入项目图像</h3>
<p><br class="spacer_" /></p>
<p>下一步你需要导入项目需要的图像。将下载的资源文件解压，并拖入称为“Resources-iPad”的文件夹下。</p>
<p>XCode 将提示你复制这些文件，选择“Yes”并按下OK。</p>
<p><br class="spacer_" /></p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.30.31-AM.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="Screen shot 2010-02-02  at 11.30.31 AM" src="http://icodeblog.com/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.30.31-AM.png" alt="" width="450" height="413" /></a></p>
<p><br class="spacer_" /></p>
<p>请包括四个图像文件以及fruits.plist。</p>
<h3>显示水果列表</h3>
<p><br class="spacer_" /></p>
<p>显示水果列表与显示其他UITableView中的数据并无区别。打开MasterViewController.h 加入水果数组定义。</p>
<pre>#import   

@class DetailViewController;   

@interface MasterViewController : UITableViewController {
    DetailViewController *detailViewController;
    NSArray * fruits;
} 

@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@property (nonatomic, retain) NSMutableArray *fruits;  

@end </pre>
<p>如你所见，这里没有任何新的东西。我们只是简单的定义了一个水果数组，并为其创建属性。</p>
<p><br class="spacer_" /></p>
<p>我们将从plist中加载水果数据。当你不想使用数据库时，从plist文件加载数据是一种方便而快捷的方法。</p>
<p><br class="spacer_" /></p>
<p>打开MasterViewController.m 在 viewDidLoad 方法中加入下列代码：</p>
<pre>- (void)viewDidLoad {
    [super viewDidLoad];
    self.fruits = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]
        pathForResource:@"fruits" ofType:@"plist"]] retain];
}</pre>
<p>fruits.plist文件实际上是一个写入文件的数组。如果你打开它，你就会发现它十分类似于XML。在为数组赋值后，我们要实现UITableView delegate 和 datasource 方法以显示列表。</p>
<p><strong> </strong></p>
<p><br class="spacer_" /></p>
<p><strong>UITableView datasource 方法</strong></p>
<pre>- (NSInteger)tableView:(UITableView *)aTableView
    numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [fruits count];
}   </pre>
<pre>- (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    // Dequeue or create a cell of the appropriate type.
    static NSString *CellIdentifier = @"CellIdentifier";  
    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
           reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }   // Get the object to display and set the value in the cell.
    cell.textLabel.text = [self.fruits objectAtIndex:indexPath.row];
    return cell;
}</pre>
<p>没有什么特殊的东西… 我们首先告诉tableview 我们需要 fruits.count (等于4) 行。然后在各tableview单元中显示水果的名称。</p>
<p><br class="spacer_" /></p>
<p><strong>UITableView delegate 方法</strong></p>
<p><strong><br />
 </strong></p>
<pre>- (void)tableView:(UITableView *)aTableView
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    /* When a row is selected, set the detail view controller's detail item
       to the item associated with the selected row.
     */
    detailViewController.detailItem = [self.fruits objectAtIndex: indexPath.row];
}
</pre>
<p>这里我们简单地将detailViewController 的detailItem 属性设置为所需水果。我们将在随后介绍一下属性，现在我们需要知道的是它的类型是 <strong>id。</strong></p>
<p><strong> </strong></p>
<p>现在，按下“Build and Run”运行一下。你应该看到如下画面：</p>
<p><br class="spacer_" /></p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/tableview.png"><img title="tableview" src="http://icodeblog.com/wp-content/uploads/2010/02/tableview.png" alt="" width="500" height="387" /></a></p>
<p><br class="spacer_" /></p>
<p>它列出了水果的列表，但当你进行选择时，没有任何变化（当然detailView的标题会改变）。</p>
<p><br class="spacer_" /></p>
<p>下面，我们需要实现显示相应水果图像的代码。</p>
<p><br class="spacer_" /></p>
<h3>显示水果</h3>
<p><br class="spacer_" /></p>
<p>显示所选水果实际上非常简单。首先我们要在detailView中加入一个UIImageView。打开DetailViewController.h 加入下列代码：</p>
<pre>@interface DetailViewController : UIViewController  {  
    UIPopoverController *popoverController;
    UINavigationBar *navigationBar;  
    id detailItem;  
    IBOutlet UIImageView * fruitImageView;
}   

@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar; 
@property (nonatomic, retain) id detailItem;  
@property (nonatomic, retain) IBOutlet UIImageView * fruitImageView;   

@end</pre>
<p>除了添加IBOutlet UIImageView外的所有代码都是由项目模板生成的。添加此行代码后，在Interface Builder中打开DetailView.xib。</p>
<p><br class="spacer_" /></p>
<p>在视窗中加入一个尺寸为500&#215;500的UIImageView。</p>
<p><br class="spacer_" /></p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/notitle.png"><img title="notitle" src="http://icodeblog.com/wp-content/uploads/2010/02/notitle.png" alt="" width="500" height="672" /></a></p>
<p><br class="spacer_" /></p>
<p>现在，选择File’s Owner 打开连接观察器 (Tools -&gt; connection inspector)。将imageView IBOutlet拖动到UIImageView 然后释放，将UIImageView 连接到你的outlet。</p>
<p><br class="spacer_" /></p>
<blockquote><p><strong>注意：如果你不希望图像变形，请将图像视窗的内容模式（content mode）设置为(在属性观察器attributes inspector中) Aspect Fit。</strong></p>
</blockquote>
<p><br class="spacer_" /></p>
<p>连接了imageview后，需要编写代码进行图像更新。关闭Interface Builder打开 DetailViewController.m 在setDetailItem 方法中加入下列代码：</p>
<pre>- (void)setDetailItem:(id)newDetailItem {
    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];   // Update the view.
        navigationBar.topItem.title = detailItem;
	NSString * imageName = [NSString stringWithFormat:@"%@.png",detailItem];
	[self.fruitImageView setImage:[UIImage imageNamed:imageName]];
    }   if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }
}</pre>
<p>大部分代码是由项目模板生成的，所以我不过多讨论。我添加的代码是根据水果的名称加载图像并随后设置其image属性。运行！下面是最终产品：</p>
<p><br class="spacer_" /></p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/cherry.png"><img title="cherry" src="http://icodeblog.com/wp-content/uploads/2010/02/cherry.png" alt="" width="500" height="387" /></a></p>
<h3>SplitViewController的另一个很酷的特性</h3>
<p><br class="spacer_" /></p>
<p>当出于垂直模式时，SplitViewController提供另一个称为UIPopOverView的新的UI元素。我们以后将就此提供教程。下面我仅给出示范图：</p>
<p><br class="spacer_" /></p>
<p><a href="http://icodeblog.com/wp-content/uploads/2010/02/popover.png"><img title="popover" src="http://icodeblog.com/wp-content/uploads/2010/02/popover.png" alt="" width="500" height="645" /></a></p>
<p><br class="spacer_" /></p>
<p>但设备处于垂直位置时，它将自动旋转视图并在“Master List”按钮按下时提供UIPopoverView。（另外，此按钮本身也是可以定制的）。</p>
<p><br class="spacer_" /></p>
<p>源码下载 <a href="http://icodeblog.com/wp-content/uploads/2010/02/iPadHelloWorld.zip">iPadHelloWorld.zip</a>.</p>
<p><br class="spacer_" /></p>
<h5>原文见：<a href="http://icodeblog.com/2010/04/05/ipad-programming-tutorial-hello-world/">iPad Programming Tutorial – Hello World++</a></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/ipad-%e7%bc%96%e7%a8%8b%e6%95%99%e7%a8%8b-hello-world/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>开始学习iphone编程之六 &#8211; 简单示例代码</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%bc%80%e5%a7%8b%e5%ad%a6%e4%b9%a0iphone%e7%bc%96%e7%a8%8b%e4%b9%8b%e5%85%ad-%e7%ae%80%e5%8d%95%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%bc%80%e5%a7%8b%e5%ad%a6%e4%b9%a0iphone%e7%bc%96%e7%a8%8b%e4%b9%8b%e5%85%ad-%e7%ae%80%e5%8d%95%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81#comments</comments>
		<pubDate>Mon, 18 Jan 2010 09:53:24 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[源代码]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=788</guid>
		<description><![CDATA[下面有一些iPhone OS 3.0的范例代码。这些代码都是没有使用Interface Builder的。作为初学者，用这种方法可以更清晰的知道程序是怎样创建用户接口的。

Window， View 和 Control
最基本的iPhone组件。
第一个程序

演示了：


设置项目并通过程序配置用户界面。
创建一个自定义类实现UIApplicationDelegate 协议。
在 main.m中设置delegate。 
创建一个UIWindow 并显示于屏幕上。
为主窗口增加一个子视图。

FirstApp源代码下载

可编辑详情视窗

提供一个文本域和一个按钮。
演示了：

使用CGRect 结构来确定子视图大小和定位。
创建一个诸如UITextField 和 UIButton的UIControl子类 。
实现处理用户触摸动作的方法。
在用户触发事件时修改 UITextField的值。
动态修改 UILabel的值。


EditableDetailView源码下载

视图动画



提供一个文本域和按钮。
演示了：

创建一个自定义 UIView 子类，它提供了自定义的动画行为。
启动 CAAnimation 。
为UIView增加一个或多个CAAffineTransform。
设置诸如动画曲线，时长和重复次数等属性。


AnimatingAView1.1源码下载

Table Views

一系列示例展示了表格视图以及导航控制器。
简单TableView


包括一个列表式UITableView，它显示了一个数组中的字符串。

演示了：

创建一个自定义UITableViewController子类。 
实现UITableViewDataSource协议要求的方法并为UITableView赋值。 
创建一个UITableViewCell并赋值。
在表格视图的各行以不同字体属性显示不同的字符串。


SimpleTableView1.1源码下载

带导航条的TableView

包括一个列表式UITableView，当用户轻触某行时进入分组式表格视图。
演示了：

通过 UINavigationController 提供不同屏幕间的导航。
实现UITableViewDelegate协议的tableViewDidSelectRowAtIndexPath: 方法来处理行选择，进入另一个视图。
通过设置字体等来控制UITableViewCell 的外观。
需要时自动加载代表 UITableView 的数据。
使用分组式 UITableView 来表示一个详情视图。


TableViewWithNavigation1.1源码下载

可编辑TableView


具有一个列表式UITableView，允许添加，删除以及行的重新排序，并允许进入包括了可编辑字段的分组式表格视图对属性进行编辑。

演示了：

创建了一个包括可编辑UITextField的自定义UITableViewCell子类。
使用UINavigationController添加新的条目到 UITableView。 
创建并改变UIImage 的大小来设置表格单元的图像。
管理表格视图的键盘，通过程序使其出现，改变键盘类型以及消失。
管理导航控制器UINavigationBar上的按钮。


EditableTableView1.1源码下载

带有文件支持的TableView

类似于可编辑TableView，但增加一些特性，包括文件支持。

演示了：

创建一个包括UITextField的自定义UITableViewCell 子类，它允许表格单元可编辑。
使用UINavigationController 增加新条目到 UITableView。 
创建并改变UIImage的大小从而设置表格单元的图像。
管理表格视图的键盘，通过程序使其出现，改变键盘类型以及消失。
管理导航控制器UINavigationBar上的按钮。


TableViewWithFilePersistence1.1源码下载

]]></description>
			<content:encoded><![CDATA[<p>下面有一些iPhone OS 3.0的范例代码。这些代码都是没有使用Interface Builder的。作为初学者，用这种方法可以更清晰的知道程序是怎样创建用户接口的。</p>
<p><span id="more-788"></span></p>
<h3 style="margin-top: 24px;">Window， View 和 Control</h3>
<p>最基本的iPhone组件。</p>
<h4>第一个程序</h4>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/FirstApp.png"><img class="alignnone size-full wp-image-790" title="FirstApp" src="http://www.iphone-geek.cn/wp-content/uploads/2010/01/FirstApp.png" alt="FirstApp" width="257" height="375" /></a></p>
<p>演示了：</p>
<p><!-- ul.nested li { padding: 5px; line-height: 18px; } --></p>
<ul>
<li>设置项目并通过程序配置用户界面。</li>
<li>创建一个自定义类实现UIApplicationDelegate 协议。</li>
<li>在 main.m中设置delegate。 </li>
<li>创建一个UIWindow 并显示于屏幕上。</li>
<li>为主窗口增加一个子视图。</li>
</ul>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/FirstApp.zip">FirstApp源代码下载</a></p>
<p><br class="spacer_" /></p>
<h4>可编辑详情视窗</h4>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/EditableDetailView.png"><img class="alignnone size-full wp-image-791" title="EditableDetailView" src="http://www.iphone-geek.cn/wp-content/uploads/2010/01/EditableDetailView.png" alt="EditableDetailView" width="256" height="375" /></a></p>
<p>提供一个文本域和一个按钮。</p>
<p>演示了：</p>
<ul>
<li>使用CGRect 结构来确定子视图大小和定位。</li>
<li>创建一个诸如UITextField 和 UIButton的UIControl子类 。</li>
<li>实现处理用户触摸动作的方法。</li>
<li>在用户触发事件时修改 UITextField的值。</li>
<li>动态修改 UILabel的值。</li>
</ul>
<p><br class="spacer_" /></p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/EditableDetailView.zip">EditableDetailView源码下载</a></p>
<p><br class="spacer_" /></p>
<h4>视图动画</p>
<p><br class="spacer_" /></p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/AnimatingAView1.png"><img class="alignnone size-full wp-image-795" title="AnimatingAView" src="http://www.iphone-geek.cn/wp-content/uploads/2010/01/AnimatingAView1.png" alt="AnimatingAView" width="257" height="375" /></a></p>
</h4>
<p>提供一个文本域和按钮。</p>
<p>演示了：</p>
<ul>
<li>创建一个自定义 UIView 子类，它提供了自定义的动画行为。</li>
<li>启动 CAAnimation 。</li>
<li>为UIView增加一个或多个CAAffineTransform。</li>
<li>设置诸如动画曲线，时长和重复次数等属性。</li>
</ul>
<p><br class="spacer_" /></p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/AnimatingAView1.1.zip">AnimatingAView1.1源码下载</a></p>
<p><br class="spacer_" /></p>
<h3>Table Views</h3>
<p><br class="spacer_" /></p>
<p>一系列示例展示了表格视图以及导航控制器。</p>
<h4>简单TableView</h4>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/SimpleTableView.png"><img class="alignnone size-full wp-image-802" title="SimpleTableView" src="http://www.iphone-geek.cn/wp-content/uploads/2010/01/SimpleTableView.png" alt="SimpleTableView" width="258" height="379" /></a></p>
<p><br class="spacer_" /></p>
<p>包括一个列表式UITableView，它显示了一个数组中的字符串。</p>
<p><br class="spacer_" /></p>
<p>演示了：</p>
<ul>
<li>创建一个自定义UITableViewController子类。 </li>
<li>实现UITableViewDataSource协议要求的方法并为UITableView赋值。 </li>
<li>创建一个UITableViewCell并赋值。</li>
<li>在表格视图的各行以不同字体属性显示不同的字符串。</li>
</ul>
<p><br class="spacer_" /></p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/SimpleTableView1.1.zip">SimpleTableView1.1源码下载</a></p>
<p><br class="spacer_" /></p>
<h4>带导航条的TableView</h4>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/TableViewWithNavigation.png"><img class="alignnone size-full wp-image-804" title="TableViewWithNavigation" src="http://www.iphone-geek.cn/wp-content/uploads/2010/01/TableViewWithNavigation.png" alt="TableViewWithNavigation" width="256" height="376" /></a></p>
<p>包括一个列表式UITableView，当用户轻触某行时进入分组式表格视图。</p>
<p>演示了：</p>
<ul>
<li>通过 UINavigationController 提供不同屏幕间的导航。</li>
<li>实现UITableViewDelegate协议的tableViewDidSelectRowAtIndexPath: 方法来处理行选择，进入另一个视图。</li>
<li>通过设置字体等来控制UITableViewCell 的外观。</li>
<li>需要时自动加载代表 UITableView 的数据。</li>
<li>使用分组式 UITableView 来表示一个详情视图。</li>
</ul>
<p><br class="spacer_" /></p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/TableViewWithNavigation1.1.zip">TableViewWithNavigation1.1源码下载</a></p>
<p><br class="spacer_" /></p>
<h4>可编辑TableView</h4>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/EditableTableView.png"><img class="alignnone size-full wp-image-806" title="EditableTableView" src="http://www.iphone-geek.cn/wp-content/uploads/2010/01/EditableTableView.png" alt="EditableTableView" width="257" height="375" /></a></p>
<p><br class="spacer_" /></p>
<p>具有一个列表式UITableView，允许添加，删除以及行的重新排序，并允许进入包括了可编辑字段的分组式表格视图对属性进行编辑。</p>
<p><br class="spacer_" /></p>
<p>演示了：</p>
<ul>
<li>创建了一个包括可编辑UITextField的自定义UITableViewCell子类。</li>
<li>使用UINavigationController添加新的条目到 UITableView。 </li>
<li>创建并改变UIImage 的大小来设置表格单元的图像。</li>
<li>管理表格视图的键盘，通过程序使其出现，改变键盘类型以及消失。</li>
<li>管理导航控制器UINavigationBar上的按钮。</li>
</ul>
<p><br class="spacer_" /></p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/EditableTableView1.1.zip">EditableTableView1.1源码下载</a></p>
<p><br class="spacer_" /></p>
<h4>带有文件支持的TableView</h4>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/TableViewWithFilePersistence.png"><img class="alignnone size-full wp-image-810" title="TableViewWithFilePersistence" src="http://www.iphone-geek.cn/wp-content/uploads/2010/01/TableViewWithFilePersistence.png" alt="TableViewWithFilePersistence" width="256" height="376" /></a></p>
<p>类似于可编辑TableView，但增加一些特性，包括文件支持。</p>
<p><br class="spacer_" /></p>
<p>演示了：</p>
<ul>
<li>创建一个包括UITextField的自定义UITableViewCell 子类，它允许表格单元可编辑。</li>
<li>使用UINavigationController 增加新条目到 UITableView。 </li>
<li>创建并改变UIImage的大小从而设置表格单元的图像。</li>
<li>管理表格视图的键盘，通过程序使其出现，改变键盘类型以及消失。</li>
<li>管理导航控制器UINavigationBar上的按钮。</li>
</ul>
<p><br class="spacer_" /></p>
<p><a href="http://www.iphone-geek.cn/wp-content/uploads/2010/01/TableViewWithFilePersistence1.1.zip">TableViewWithFilePersistence1.1源码下载</a></p>
<p><br class="spacer_" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%bc%80%e5%a7%8b%e5%ad%a6%e4%b9%a0iphone%e7%bc%96%e7%a8%8b%e4%b9%8b%e5%85%ad-%e7%ae%80%e5%8d%95%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>iPhone 3.0新特性系列教程之一 &#8211; 使用UIPasteboard复制/粘帖</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/iphone-3-0%e6%96%b0%e7%89%b9%e6%80%a7%e7%b3%bb%e5%88%97%e6%95%99%e7%a8%8b%e4%b9%8b%e4%b8%80-%e4%bd%bf%e7%94%a8uipasteboard%e5%a4%8d%e5%88%b6%e7%b2%98%e5%b8%96</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/iphone-3-0%e6%96%b0%e7%89%b9%e6%80%a7%e7%b3%bb%e5%88%97%e6%95%99%e7%a8%8b%e4%b9%8b%e4%b8%80-%e4%bd%bf%e7%94%a8uipasteboard%e5%a4%8d%e5%88%b6%e7%b2%98%e5%b8%96#comments</comments>
		<pubDate>Mon, 23 Nov 2009 05:56:40 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=311</guid>
		<description><![CDATA[
今天的主题是怎样读/写粘帖板。
&#160;

粘帖板概述
&#160;
多粘帖板
&#160;
iPhone上不只一个粘帖板：
有两个系统粘帖板：一个通用系统粘帖板用于复制/粘贴操作，一个查找粘帖板保存上一次搜寻用的字符串。
另外，应用程序还可以创建自己的粘帖板，甚至可以被其他应用程序使用。例如，零售机程序以及信用卡终端程序可以使用共享粘帖板来传递支付信息。
&#160;
多重表示
&#160;
加入到粘帖板的条目都有一个类型。更准确地说，当一个条目加入到粘帖板时，都有一个或多个类型的表示。比如，一个网页地址可以存储为字符串或一个URL。
&#160;
在粘帖板中存储多种表示的好处是带来了条目使用的灵活性。一个email客户端程序可以使用文本表示将URL的NSString注入到邮件主体中；而播客（podcast）可以通过NSURL表示获取播客内容。
&#160;
这种处理方法并非iPhone独有。这种设计可以使Safari中的复制操作在TextMate中产生普通文本而在页面中产生格式化好的文本。
&#160;
粘帖板操作
&#160;
注意：虽然本文中的范例使用通用粘帖板，但因为所有粘帖板都是UIPasteboard的子类，编程方法适用于所有粘帖板。
&#160;
使用通用粘帖板
&#160;
generalPasteboard类方法返回一个通用粘帖板的指针，例如：
1&#160; &#160; UIPasteboard *pasteboard = &#91;UIPasteboard generalPasteboard&#93;;
&#160;
常用属性
&#160;
最常用的粘帖板操作包括获取/设置字符串，图像，URL和颜色。Apple提供了以下方便的方法：
12345NSString *string = pasteboard.string; &#160;
UIImage *image = pasteboard.image; &#160;
NSURL *url = pasteboard.URL; &#160;
UIColor *color = pasteboard.color; &#160; &#160;
pasteboard.string = @&#34;paste me somewhere&#34;;
&#160;
确定一种类型的表示是否存在
&#160;
如果某种类型的条目不存在，那么getter方法将返回nil。这是一个很方便的方法可以预先知道特定类型的表示是否存在。使用containsPasteboardTypes: 方法检查：
12if &#40;&#91;pasteboard containsPasteboardTypes: &#91;NSArray arrayWithObject:@&#34;public.utf8-plain-text&#34;&#93;&#93;&#41; &#160;&#160; &#160; 
&#160; &#160; NSLog&#40;@&#34;String representation present: %@&#34;, pasteboard.string&#41;;
&#160;
使用pasteboardTypes可获取一个条目的各种类型。
&#160;
统一类型标志符 — UTI
&#160;
上例中通过检查public.utf8-plain-text类型来确定字符串是否存在。本例将使用UTI（通用类型标志符）。有关UTI信息，请见Apple的 通用类型标志符概述文档。
&#160;
Apple在UTCoreTypes.h中提供了通用类型的常量。要使用这些常量：


在项目中加入MobileCoreServices framework
#import &#60;MobileCoreServices/MobileCoreServices.h&#62; 


&#160;
常量的类型是CFStringRef。你可以把它们当作NSString。CFStringRef是一种C和Objective-C通用的表达方式。为在Objective-C使用它，只需简单的转换操作：
1NSString *urlUTIType = &#40;NSString *&#41;kUTTypeURL;
&#160;
你可以自由构造自己的数据类型；为保证独特性，Apple推荐使用reverse-DNS标记式样（如com.mobileorchard.mySnazzyType）。
&#160;
设置/提取缺乏Getter/Setter的类型
&#160;
Getter/Setter只是为了提供方便。实际上，它们与使用valueForPasteboardType: 和 setValue:forPasteboardType 方法没什么区别：
12NSString [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.mobileorchard.com/wp-content/uploads/2009/07/copy.gif" alt="" width="250"></p>
<p>今天的主题是怎样读/写粘帖板。</p>
<p>&nbsp;</p>
<p><span id="more-311"></span></p>
<h2>粘帖板概述</h2>
<p>&nbsp;</p>
<h3><strong>多粘帖板</strong></h3>
<p>&nbsp;</p>
<p>iPhone上不只一个粘帖板：</p>
<p>有两个系统粘帖板：一个<em>通用</em>系统粘帖板用于复制/粘贴操作，一个<em>查找</em>粘帖板保存上一次搜寻用的字符串。</p>
<p>另外，应用程序还可以创建自己的粘帖板，甚至可以被其他应用程序使用。例如，<a href="http://www.pingysoft.com/site/Ring_It_Up.html">零售机程序</a>以及<a href="http://www.innerfence.com/apps/credit-card-terminal">信用卡终端程序</a>可以使用共享粘帖板来传递支付信息。</p>
<p>&nbsp;</p>
<h3><strong>多重表示</strong></h3>
<p>&nbsp;</p>
<p>加入到粘帖板的条目都有一个<em>类型</em>。更准确地说，当一个条目加入到粘帖板时，都有一个或多个类型的表示。比如，一个网页地址可以存储为字符串或一个URL。</p>
<p>&nbsp;</p>
<p>在粘帖板中存储多种表示的好处是带来了条目使用的灵活性。一个email客户端程序可以使用文本表示将URL的NSString注入到邮件主体中；而播客（podcast）可以通过NSURL表示获取播客内容。</p>
<p>&nbsp;</p>
<p>这种处理方法并非iPhone独有。这种设计可以使Safari中的复制操作在TextMate中产生普通文本而在页面中产生格式化好的文本。</p>
<p>&nbsp;</p>
<h2>粘帖板操作</h2>
<p>&nbsp;</p>
<p>注意：虽然本文中的范例使用通用粘帖板，但因为所有粘帖板都是UIPasteboard的子类，编程方法适用于所有粘帖板。</p>
<p>&nbsp;</p>
<h3><strong>使用通用粘帖板</strong></h3>
<p>&nbsp;</p>
<p>generalPasteboard类方法返回一个通用粘帖板的指针，例如：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; UIPasteboard <span style="color: #002200;">*</span>pasteboard <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIPasteboard generalPasteboard<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3><strong>常用属性</strong></h3>
<p>&nbsp;</p>
<p>最常用的粘帖板操作包括获取/设置字符串，图像，URL和颜色。Apple提供了以下方便的方法：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> pasteboard.<span style="color: #a61390;">string</span>; &nbsp;<br />
UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> pasteboard.image; &nbsp;<br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> pasteboard.URL; &nbsp;<br />
UIColor <span style="color: #002200;">*</span>color <span style="color: #002200;">=</span> pasteboard.color; &nbsp; &nbsp;<br />
pasteboard.<span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;paste me somewhere&quot;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3><strong>确定一种类型的表示是否存在</strong></h3>
<p>&nbsp;</p>
<p>如果某种类型的条目不存在，那么getter方法将返回nil。这是一个很方便的方法可以预先知道特定类型的表示是否存在。使用containsPasteboardTypes: 方法检查：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>pasteboard containsPasteboardTypes<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> arrayWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;public.utf8-plain-text&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> &nbsp;&nbsp; &nbsp; <br />
&nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;String representation present: %@&quot;</span>, pasteboard.<span style="color: #a61390;">string</span><span style="color: #002200;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>使用pasteboardTypes可获取一个条目的各种类型。</p>
<p>&nbsp;</p>
<h3><strong>统一类型标志符 — UTI</strong></h3>
<p>&nbsp;</p>
<p>上例中通过检查<em>public.utf8-plain-text</em>类型来确定字符串是否存在。本例将使用UTI（通用类型标志符）。有关UTI信息，请见Apple的<a href="http://developer.apple.com/documentation/Carbon/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html"> 通用类型标志符概述</a>文档。</p>
<p>&nbsp;</p>
<p>Apple在UTCoreTypes.h中提供了通用类型的常量。要使用这些常量：</p>
<blockquote>
<ol>
<li>在项目中加入MobileCoreServices framework</li>
<li>#import &lt;MobileCoreServices/MobileCoreServices.h&gt; </li>
</ol>
</blockquote>
<p>&nbsp;</p>
<p>常量的类型是CFStringRef。你可以把它们当作NSString。CFStringRef是一种C和Objective-C通用的表达方式。为在Objective-C使用它，只需简单的转换操作：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>urlUTIType <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>kUTTypeURL;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>你可以自由构造自己的数据类型；为保证独特性，Apple推荐使用reverse-DNS标记式样（如<em>com.mobileorchard.mySnazzyType</em>）。</p>
<p>&nbsp;</p>
<h3><strong>设置/提取缺乏Getter/Setter的类型</strong></h3>
<p>&nbsp;</p>
<p>Getter/Setter只是为了提供方便。实际上，它们与使用valueForPasteboardType: 和 setValue:forPasteboardType 方法没什么区别：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pasteboard valueForPasteboardType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;public.utf8-plain-text&quot;</span><span style="color: #002200;">&#93;</span>; &nbsp;<br />
<span style="color: #002200;">&#91;</span>pasteBoard setValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;paste me somewhere&quot;</span> forPasteboardType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;public.utf8-plain-text&quot;</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>此方法适用于字符串，数组，字典，日期，数值和URL。而dataForPasteboardType 和 setData:forPasteboardType: 方法适用于其它各种类型。</p>
<p>&nbsp;</p>
<h3><strong>以多种表示存储条目</strong></h3>
<p>&nbsp;</p>
<p>使用方便的setter有一个很大的限制：条目只能以一种类型存储。而如果使用上述方法，我们可以将URL存为NSString和NSURL，以构成一个类型和值的字典并设置其条目属性：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">static</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.mobileorchard.com&quot;</span>; &nbsp;<br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span>item <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #a61390;">string</span>, <br />
&nbsp; &nbsp; <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;public.utf8-plain-text&quot;</span>, <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> URLWithString<span style="color: #002200;">:</span><span style="color: #a61390;">string</span><span style="color: #002200;">&#93;</span>, &nbsp;&nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>kUTTypeURL, &nbsp; &nbsp; <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; &nbsp;<br />
pasteboard.items <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> arrayWithObject<span style="color: #002200;">:</span>item<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>现在，pasteboard.string和pasteboard.url都将有内容返回了。</p>
<p>&nbsp;</p>
<h3><strong>粘帖板支持多条目</strong></h3>
<p>&nbsp;</p>
<p>请注意，在上例中，我们使用了<em>items</em> —  复数形式 — 属性。为保持本教程的简单性，我将粘帖板视为一个单条目实体。如果你需要，你可以获取/设置粘帖板的条目数组。这实际上很简单，请参见文档。</p>
<p>&nbsp;</p>
<h5>原文见：<a href="http://www.mobileorchard.com/new-in-iphone-30-tutorial-series-part-3-copy-paste-with-uipasteboard/">New In iPhone 3.0 Tutorial Series, Part 3: Copy &amp; Paste With UIPasteboard</a>&nbsp;作者：<a href="http://www.mobileorchard.com/author/dgrigsby/">Dan Grigsby</a><br />
      </h5>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/iphone-3-0%e6%96%b0%e7%89%b9%e6%80%a7%e7%b3%bb%e5%88%97%e6%95%99%e7%a8%8b%e4%b9%8b%e4%b8%80-%e4%bd%bf%e7%94%a8uipasteboard%e5%a4%8d%e5%88%b6%e7%b2%98%e5%b8%96/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在UIAlertView中加入UITextField</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%9c%a8uialertview%e4%b8%ad%e5%8a%a0%e5%85%a5uitextfield</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%9c%a8uialertview%e4%b8%ad%e5%8a%a0%e5%85%a5uitextfield#comments</comments>
		<pubDate>Sun, 22 Nov 2009 04:05:52 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[UIAlertView]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=283</guid>
		<description><![CDATA[
&#160;
这个简单的教程将介绍怎样在UIAlertView中加入UITextField。它只包括几行代码。你将学到CGAffineTransform和UITextField编程。
&#160;

这是截屏。

&#160;
我们开始吧…
1. 建立一个新的View Based项目。
你可以随意为其取名，我为其命名为TextFieldInAlert。
2. 代码
在viewcontroller.m或TextFieldInAlert.m（如果你命名为TextFieldInAlert的话），然后找到-(void)viewDidLoad 方法。取消其注释加入代码。
123456789101112131415161718192021- &#40;void&#41;viewDidLoad &#123;

&#160; &#160; &#91;super viewDidLoad&#93;;

&#160; &#160; UIAlertView *alert = &#91;&#91;UIAlertView alloc&#93; initWithTitle:@”Enter Name Here” 
&#160; &#160; &#160; &#160; message:@”this gets covered!” delegate:self 
&#160; &#160; &#160; &#160; cancelButtonTitle:@”Dismiss” otherButtonTitles:@”OK!”, nil&#93;;

&#160; &#160; UITextField *myTextField = &#91;&#91;UITextField alloc&#93; 
&#160; &#160; &#160; &#160; initWithFrame:CGRectMake&#40;12, 45, 260, 25&#41;&#93;;

&#160; &#160; &#91;myTextField setBackgroundColor:&#91;UIColor whiteColor&#93;&#93;;

&#160; &#160; &#91;alert addSubview:myTextField&#93;;

&#160; [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://icodeblog.com/wp-content/uploads/2009/11/Screen-shot-2009-11-09-at-8.12.11-AM-copy.png" alt="Screen shot 2009-11-09 at 8.12.11 AM copy" width="200" height="96" /></p>
<p>&nbsp;</p>
<p>这个简单的教程将介绍怎样在UIAlertView中加入UITextField。它只包括几行代码。你将学到CGAffineTransform和UITextField编程。</p>
<p>&nbsp;</p>
<p><span id="more-283"></span><br />
这是截屏。</p>
<p><img src="http://icodeblog.com/wp-content/uploads/2009/11/Screen-shot-2009-11-09-at-8.12.11-AM.png" alt="Screen shot 2009-11-09 at 8.12.11 AM" width="260" height="388" /></p>
<p>&nbsp;</p>
<p>我们开始吧…</p>
<p>1. 建立一个新的View Based项目。<br />
你可以随意为其取名，我为其命名为TextFieldInAlert。</p>
<p>2. 代码<br />
在viewcontroller.m或TextFieldInAlert.m（如果你命名为TextFieldInAlert的话），然后找到-(void)viewDidLoad 方法。取消其注释加入代码。</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span>@”Enter Name Here” <br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #002200;">:</span>@”this <a href="http://www.opengroup.org/onlinepubs/009695399/functions/gets.html"><span style="color: #a61390;">gets</span></a> covered<span style="color: #002200;">!</span>” delegate<span style="color: #002200;">:</span>self <br />
&nbsp; &nbsp; &nbsp; &nbsp; cancelButtonTitle<span style="color: #002200;">:</span>@”Dismiss” otherButtonTitles<span style="color: #002200;">:</span>@”OK<span style="color: #002200;">!</span>”, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; UITextField <span style="color: #002200;">*</span>myTextField <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextField alloc<span style="color: #002200;">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>12, 45, 260, 25<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myTextField setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert addSubview<span style="color: #002200;">:</span>myTextField<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myTextField release<span style="color: #002200;">&#93;</span>;<br />
<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>我们做的是调用UIAlertView然后添加一个UITextField。 你可能注意到了UIAlertView的message部分，我们为其赋值为“this gets covered!”，如果我们不为其加上这个语句，那么alert的按钮会更靠上而弄乱UITextField。你可以试一下拿掉这一行看看会发生什么。现在UITextField 出现在UIAlertView中间了。试一下在UITextField输入点什么。噢，为什么键盘盖住了UIAlertView？有一个简单的修正方法。只需加入两行代码就可修正这个问题。加入代码：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; CGAffineTransform myTransform <span style="color: #002200;">=</span> CGAffineTransformMakeTranslation<span style="color: #002200;">&#40;</span>0, 60<span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert setTransform<span style="color: #002200;">:</span>myTransform<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>完整代码如下：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span>@”Enter Name Here” <br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #002200;">:</span>@”this <a href="http://www.opengroup.org/onlinepubs/009695399/functions/gets.html"><span style="color: #a61390;">gets</span></a> covered<span style="color: #002200;">!</span>” delegate<span style="color: #002200;">:</span>self <br />
&nbsp; &nbsp; &nbsp; &nbsp; cancelButtonTitle<span style="color: #002200;">:</span>@”Dismiss” otherButtonTitles<span style="color: #002200;">:</span>@”OK<span style="color: #002200;">!</span>”, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; UITextField <span style="color: #002200;">*</span>myTextField <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextField alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>12, 45, 260, 25<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; CGAffineTransform myTransform <span style="color: #002200;">=</span> CGAffineTransformMakeTranslation<span style="color: #002200;">&#40;</span>0, 60<span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert setTransform<span style="color: #002200;">:</span>myTransform<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myTextField setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert addSubview<span style="color: #002200;">:</span>myTextField<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myTextField release<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>现在你按下“Build and Run”，你会注意到UITextField将在显示略高一点的位置，当你在UITextField输入时，键盘不会再盖住。这就是CGAffineTransform的作用。这里还有一个<a href="http://www.youtube.com/watch?v=1BvuD6MAPmU">视频教程</a>。源代码如下。</p>
<p>&nbsp;</p>
<p><a onclick="pageTracker._trackPageview('/ou[/cc]tgoing/xcode.appstoremod.com/TextFieldInAlert.zip?referer=http%3A%2F%2Ficodeblog.com%2F');" href="http://xcode.appstoremod.com/TextFieldInAlert.zip">iPhone Tutorial – UITextField In A UIAlertView</a></p>
<p>&nbsp;</p>
<p>原文见：<a title="Permanent Link to iPhone Coding Tutorial – Inserting A UITextField In A UIAlertView" rel="bookmark" href="http://icodeblog.com/2009/11/09/iphone-coding-tutorial-inserting-a-uitextfield-in-a-uialertview/">iPhone Coding Tutorial – Inserting A UITextField In A UIAlertView </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%9c%a8uialertview%e4%b8%ad%e5%8a%a0%e5%85%a5uitextfield/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UILabelz子类 &#8211; 随意对齐文本</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/uilabelz%e5%ad%90%e7%b1%bb-%e9%9a%8f%e6%84%8f%e5%af%b9%e9%bd%90%e6%96%87%e6%9c%ac</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/uilabelz%e5%ad%90%e7%b1%bb-%e9%9a%8f%e6%84%8f%e5%af%b9%e9%bd%90%e6%96%87%e6%9c%ac#comments</comments>
		<pubDate>Sun, 15 Nov 2009 11:30:03 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=243</guid>
		<description><![CDATA[有人问到怎样正确地对齐UILabel的文字。他们需要UILabel容纳多行文本，但也需要调整文本，他们遇到的问题是单行文本在垂直方向总是居中对齐。  
&#160;

在apple论坛上: 
http://discussions.apple.com/thread.jspa?threadID=1759957有人提供的UILabel子类可以解决这个问题：
此子类如下：
123456789101112131415161718192021&#160; &#160; // Label2.h
&#160; &#160; // (c) 2009 Ivan Misuno, www.cuberoom.biz&#60;br&#62;
&#160; &#160; 
&#160; &#160; #import &#60;UIKit/UIKit.h&#62;

&#160; &#160; typedef enum
&#160; &#160; &#123;
&#160; &#160; &#160; &#160; VerticalAlignmentTop = 0, // default
&#160; &#160; &#160; &#160; VerticalAlignmentMiddle,
&#160; &#160; &#160; &#160; VerticalAlignmentBottom,
&#160; &#160; &#125; VerticalAlignment;
&#160; &#160; &#160; 
&#160; &#160; @interface Label2 : UILabel
&#160; &#160; &#123;
&#160; &#160; @private
&#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>有人问到怎样正确地对齐UILabel的文字。他们需要UILabel容纳多行文本，但也需要调整文本，他们遇到的问题是单行文本在垂直方向总是居中对齐。  </p>
<p>&nbsp;</p>
<p><span id="more-243"></span></p>
<p>在apple论坛上: <a onclick="javascript:pageTracker._trackPageview('/outgoing/discussions.apple.com/thread.jspa?threadID=1759957');" rel="nofollow" target="_blank" href="http://discussions.apple.com/thread.jspa?threadID=1759957"><br />
http://discussions.apple.com/thread.jspa?threadID=1759957</a>有人提供的UILabel子类可以解决这个问题：</p>
<p>此子类如下：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Label2.h</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// (c) 2009 Ivan Misuno, www.cuberoom.biz&lt;br&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">typedef</span> <span style="color: #a61390;">enum</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; VerticalAlignmentTop <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>, <span style="color: #11740a; font-style: italic;">// default</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; VerticalAlignmentMiddle,<br />
&nbsp; &nbsp; &nbsp; &nbsp; VerticalAlignmentBottom,<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span> VerticalAlignment;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">@interface</span> Label2 <span style="color: #002200;">:</span> UILabel<br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">@private</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; VerticalAlignment _verticalAlignment;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic<span style="color: #002200;">&#41;</span> VerticalAlignment verticalAlignment;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Label2.mm</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// (c) 2009 Ivan Misuno, www.cuberoom.biz</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #6e371a;">#import &quot;Label2.h&quot;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #a61390;">@implementation</span> Label2<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame<br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>self<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; _verticalAlignment <span style="color: #002200;">=</span> VerticalAlignmentTop;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> self;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc<br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>VerticalAlignment<span style="color: #002200;">&#41;</span> verticalAlignment<br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> _verticalAlignment;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setVerticalAlignment<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>VerticalAlignment<span style="color: #002200;">&#41;</span>value<br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _verticalAlignment <span style="color: #002200;">=</span> value;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#91;</span>self setNeedsDisplay<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// align text block according to vertical alignment settings</span><br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>textRectForBounds<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>bounds limitedToNumberOfLines<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfLines<br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CGRect rect <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super textRectForBounds<span style="color: #002200;">:</span>bounds limitedToNumberOfLines<span style="color: #002200;">:</span>numberOfLines<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; CGRect result;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">switch</span><span style="color: #002200;">&#40;</span>_verticalAlignment<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> VerticalAlignmentTop<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>bounds.origin.x, bounds.origin.y, rect.size.width, rect.size.height<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> VerticalAlignmentMiddle<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>bounds.origin.x, bounds.origin.y <span style="color: #002200;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#40;</span>bounds.size.height <span style="color: #002200;">-</span> rect.size.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> 2, rect.size.width, &nbsp;rect.size.height<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> VerticalAlignmentBottom<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>bounds.origin.x, bounds.origin.y <span style="color: #002200;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#40;</span>bounds.size.height <span style="color: #002200;">-</span> rect.size.height<span style="color: #002200;">&#41;</span>, rect.size.width, &nbsp;rect.size.height<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">default</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #002200;">=</span> bounds;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> result;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawTextInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect<br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CGRect r <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self textRectForBounds<span style="color: #002200;">:</span>rect limitedToNumberOfLines<span style="color: #002200;">:</span>self.numberOfLines<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super drawTextInRect<span style="color: #002200;">:</span>r<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>你可以在使用UILabel的地方使用Label2。可以通过将setVerticalAlignment设为VerticalAlignmentTop,  VerticalAlignmentMiddle, 或 VerticalAlignmentBottom的方法将文本对齐在顶部，中间或底部。十分方便！
</p>
<p>&nbsp;</p>
<h5>原文见：<a href="http://maniacdev.com/2009/09/uilabel-subclass-align-the-text-how-you-want/" rel="bookmark" title="Permanent Link: UILabel Subclass – Align The Text How You Want!">UILabel Subclass – Align The Text How You Want!</a></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/uilabelz%e5%ad%90%e7%b1%bb-%e9%9a%8f%e6%84%8f%e5%af%b9%e9%bd%90%e6%96%87%e6%9c%ac/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>不用Interface Builder的iPhone项目</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%b8%8d%e7%94%a8interface-builder%e7%9a%84iphone%e9%a1%b9%e7%9b%ae</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%b8%8d%e7%94%a8interface-builder%e7%9a%84iphone%e9%a1%b9%e7%9b%ae#comments</comments>
		<pubDate>Fri, 13 Nov 2009 04:25:38 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=227</guid>
		<description><![CDATA[有初学iPhone编程的朋友问我，不用Interface Builder可以编写iPhone程序吗？答案当然是：可以。
&#160;

Interface Builder是一个用户界面工具，功能强大。不过对比其他一些界面建造工具如.net，Java，VB，甚至是wxWidget的界面工具，它不太直观。所以新手有可能觉得不太好用。（我觉得Interface Builder与其他一些界面工具最大区别是，它不直接产生代码）。不过如果仅仅是因为这个原因，而你又想成为iPhone编程高手，特别是编写一些非游戏类程序，我的建议是读一些有关Interface Builder的教程或书籍，掌握Interface Builder是必须的。当然，游戏游戏有自己的界面，不需要使用Interface Builder。

Interface Builder直接生成的是.xib（以前叫.nib）文件（按惯例我就叫它nib文件），它将你的界面以object的形式存存储在文件中。我们知道每个iPhone（Xcode）项目都有一个.plist文件，其中有一项叫做“Main nib file base name”，它指向了程序启动时要调用的nib文件MainWindow.xib。关于Interface Builder的工作流程，以及启动时的调用我将在另一篇文章中介绍，在此我就不多罗嗦了。
&#160;

总而言之，每个Object-C的程序都像c或c++一样有一个main函数，它是整个程序的入口（在main.m中）。iPhone程序的main函数中，调用了iPhone程序的UIApplicationMain。它的格式是：
123456int UIApplicationMain &#40;
&#160; &#160;int argc,
&#160; &#160;char *argv&#91;&#93;,
&#160; &#160;NSString *principalClassName,
&#160; &#160;NSString *delegateClassName
&#41;;
&#160;

第一，二个参数，如果熟悉c/c++的朋友一定很清楚，它们是用来传递命令行参数的，iPhone程序一般不用。principalClassName指向UIApplication类或之类。delegateClassName指向遵循UIApplicationDelegate协议的delegate类。
&#160;

如果我们不想使用Interface Builder，我们要做的就是：
&#160;

1. 将plist文件中的“Main nib file base name”项删除。
2. 删除了“Main nib file base name”项后，如果principalClassName，delegateClassName为nil的话，程序就无以为继了，其结果就是显示黑屏。所以，我们必须用principalClassName或delegateClassName参数告诉iPhone我们程序的切入点。有两种方式。如果你建立一个UIApplication的子类，那么你就要用principalClassName来指定。比如你建立一个Application类，示例如下：
1234@interface Application : UIApplication
&#123;
&#125;
@end
&#160;

你必须这样调用UIApplicationMain：
1UIApplicationMain&#40;argc,argv,@&#34;Application&#34;,nil&#41;;
&#160;

第二种方式是，你创建一个代理类，示例如下：
&#160;
1234@interface AppDelegate : NSObject&#60;UIApplicationDelegate&#62;
&#123;
&#125;
@end
&#160;

你必须这样调用UIApplicationMain:
1UIApplicationMain&#40;argc,argv,nil,@&#34;AppDelegate&#34;&#41;;
&#160;

这样iPhone就知道你程序的真正入口了。当然，剩下的工作就需要你自己完成了，比如自己创建View Controller或直接创建View，或者干脆直接调用你的游戏引擎，不过不要忘记在applicationDidFinishLaunching中创建一个UIWindow。
&#160;

为了方便我建立了一个项目模板，此模板包括一个AppDelegate和一个MainViewController。使用此模板，你所有的工作只需从MainViewController的loadView或viewDidLoad中开始。
&#160;

NoIBProject模板下载
&#160;

你可以直接解压使用，或者将其解压到/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application/下作为Xcode模板使用。
]]></description>
			<content:encoded><![CDATA[<p>有初学iPhone编程的朋友问我，不用Interface Builder可以编写iPhone程序吗？答案当然是：可以。</p>
<p>&nbsp;
<p>
Interface Builder是一个用户界面工具，功能强大。不过对比其他一些界面建造工具如.net，Java，VB，甚至是wxWidget的界面工具，它不太直观。所以新手有可能觉得不太好用。（我觉得Interface Builder与其他一些界面工具最大区别是，它不直接产生代码）。不过如果仅仅是因为这个原因，而你又想成为iPhone编程高手，特别是编写一些非游戏类程序，我的建议是读一些有关Interface Builder的教程或书籍，掌握Interface Builder是必须的。当然，游戏游戏有自己的界面，不需要使用Interface Builder。<br />
<span id="more-227"></span><br />
Interface Builder直接生成的是.xib（以前叫.nib）文件（按惯例我就叫它nib文件），它将你的界面以object的形式存存储在文件中。我们知道每个iPhone（Xcode）项目都有一个.plist文件，其中有一项叫做“Main nib file base name”，它指向了程序启动时要调用的nib文件MainWindow.xib。关于Interface Builder的工作流程，以及启动时的调用我将在另一篇文章中介绍，在此我就不多罗嗦了。</p>
<p>&nbsp;
<p>
总而言之，每个Object-C的程序都像c或c++一样有一个main函数，它是整个程序的入口（在main.m中）。iPhone程序的main函数中，调用了iPhone程序的UIApplicationMain。它的格式是：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">int</span> UIApplicationMain <span style="color: #002200;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #a61390;">int</span> argc,<br />
&nbsp; &nbsp;<span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span>,<br />
&nbsp; &nbsp;<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>principalClassName,<br />
&nbsp; &nbsp;<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>delegateClassName<br />
<span style="color: #002200;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;
<p>
第一，二个参数，如果熟悉c/c++的朋友一定很清楚，它们是用来传递命令行参数的，iPhone程序一般不用。principalClassName指向UIApplication类或之类。delegateClassName指向遵循UIApplicationDelegate协议的delegate类。</p>
<p>&nbsp;
<p>
如果我们不想使用Interface Builder，我们要做的就是：</p>
<p>&nbsp;
<p>
1. 将plist文件中的“Main nib file base name”项删除。<br />
2. 删除了“Main nib file base name”项后，如果principalClassName，delegateClassName为nil的话，程序就无以为继了，其结果就是显示黑屏。所以，我们必须用principalClassName或delegateClassName参数告诉iPhone我们程序的切入点。有两种方式。如果你建立一个UIApplication的子类，那么你就要用principalClassName来指定。比如你建立一个Application类，示例如下：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> Application <span style="color: #002200;">:</span> UIApplication<br />
<span style="color: #002200;">&#123;</span><br />
<span style="color: #002200;">&#125;</span><br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>&nbsp;
<p>
你必须这样调用UIApplicationMain：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">UIApplicationMain<span style="color: #002200;">&#40;</span>argc,argv,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Application&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;
<p>
第二种方式是，你创建一个代理类，示例如下：</p>
<p>&nbsp;
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> AppDelegate <span style="color: #002200;">:</span> NSObject&lt;UIApplicationDelegate&gt;<br />
<span style="color: #002200;">&#123;</span><br />
<span style="color: #002200;">&#125;</span><br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>&nbsp;
<p>
你必须这样调用UIApplicationMain:</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">UIApplicationMain<span style="color: #002200;">&#40;</span>argc,argv,<span style="color: #a61390;">nil</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;AppDelegate&quot;</span><span style="color: #002200;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>&nbsp;
<p>
这样iPhone就知道你程序的真正入口了。当然，剩下的工作就需要你自己完成了，比如自己创建View Controller或直接创建View，或者干脆直接调用你的游戏引擎，不过不要忘记在applicationDidFinishLaunching中创建一个UIWindow。</p>
<p>&nbsp;
<p>
为了方便我建立了一个项目模板，此模板包括一个AppDelegate和一个MainViewController。使用此模板，你所有的工作只需从MainViewController的loadView或viewDidLoad中开始。</p>
<p>&nbsp;
<p>
<a href='http://www.iphone-geek.cn/wp-content/uploads/2009/11/NoIBProject.zip'>NoIBProject模板下载</a></p>
<p>&nbsp;
<p>
你可以直接解压使用，或者将其解压到/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application/下作为Xcode模板使用。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%b8%8d%e7%94%a8interface-builder%e7%9a%84iphone%e9%a1%b9%e7%9b%ae/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>定制iPhone键盘</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%ae%9a%e5%88%b6iphone%e9%94%ae%e7%9b%98</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%ae%9a%e5%88%b6iphone%e9%94%ae%e7%9b%98#comments</comments>
		<pubDate>Wed, 11 Nov 2009 08:21:03 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=174</guid>
		<description><![CDATA[想在你的游戏或应用程序中，改变一下你键盘的形象或定制特殊的键盘，方法很简单。我编写了一个简单程序将键盘上的Q键改为！键（由于photoshop水平有限，不是很完美，见谅！）



思路如下：

1. 键盘出现前都会系统会发出通知，我们要做的就是截取此事件，然后获得UIKeyboard对象：
1&#91;&#91;NSNotificationCenter defaultCenter&#93; addObserver:self selector:@selector&#40;keyboardWillShow:&#41; name:UIKeyboardWillShowNotification object:nil&#93;;

2. 获取UIKeyboard对象。UIKeyboard处于UIWindow中。但是要注意的是，它并不处于当前应用程序的UIWindow中。所以我们必须通过一个循环来查找所需的UIWindow，从而获得UIKeyboard。然后替换或添加UIButton作为定制的按键：
1234567891011121314151617181920212223242526272829303132333435363738- &#40;void&#41;keyboardWillShow:&#40;NSNotification *&#41;notification
&#123;
&#160; &#160; UIWindow* tmpWindow;&#160; &#160; 
&#160; &#160; UIView* keyboard;
&#160; &#160; 
&#160; &#160; // Check each window in our application
&#160; &#160; for&#40;int i = 0; i &#60; &#91;&#91;&#91;UIApplication sharedApplication&#93; windows&#93; count&#93;; i++&#41;
&#160; &#160; &#123;
&#160; &#160; &#160; &#160; // Get a reference of the current window
&#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>想在你的游戏或应用程序中，改变一下你键盘的形象或定制特殊的键盘，方法很简单。我编写了一个简单程序将键盘上的Q键改为！键（由于photoshop水平有限，不是很完美，见谅！）<br />
<span id="more-174"></span><br />
<a href="http://www.iphone-geek.cn/wp-content/uploads/2009/11/screen.png"><img src="http://www.iphone-geek.cn/wp-content/uploads/2009/11/screen.png" alt="screen" title="screen" width="493" height="755" class="alignnone size-full wp-image-179" /></a><br />
<br />
思路如下：<br />
<br />
1. 键盘出现前都会系统会发出通知，我们要做的就是截取此事件，然后获得UIKeyboard对象：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span style="color: #400080;">NSNotificationCenter</span></a> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>keyboardWillShow<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span>UIKeyboardWillShowNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>
2. 获取UIKeyboard对象。UIKeyboard处于UIWindow中。但是要注意的是，它并不处于当前应用程序的UIWindow中。所以我们必须通过一个循环来查找所需的UIWindow，从而获得UIKeyboard。然后替换或添加UIButton作为定制的按键：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>keyboardWillShow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/"><span style="color: #400080;">NSNotification</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; UIWindow<span style="color: #002200;">*</span> tmpWindow;&nbsp; &nbsp; <br />
&nbsp; &nbsp; UIView<span style="color: #002200;">*</span> keyboard;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Check each window in our application</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> windows<span style="color: #002200;">&#93;</span> count<span style="color: #002200;">&#93;</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Get a reference of the current window</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tmpWindow <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> windows<span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> j <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; j &lt; <span style="color: #002200;">&#91;</span>tmpWindow.subviews count<span style="color: #002200;">&#93;</span>; j<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyboard <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpWindow.subviews objectAtIndex<span style="color: #002200;">:</span>j<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// From all the apps i have made, they keyboard view description always starts with &lt;UIKeyboard so I did the following</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>keyboard description<span style="color: #002200;">&#93;</span> hasPrefix<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&lt;UIKeyboard&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIButton<span style="color: #002200;">*</span> key <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Position the button - I found these numbers align fine (0, 0 = top left of keyboard)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>2, 10, 31, 46<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Add images to our button so that it looks just like a native UI Element.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>key setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;key.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>key setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;key-pressed.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateHighlighted<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//Add the button to the keyboard</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>keyboard addSubview<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// When the decimal button is pressed, we send a message to ourself (the AppDelegate) which will then post a notification that will then append a decimal in the UITextField in the Appropriate View Controller.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>key addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>keypressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> &nbsp;forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span> &nbsp; <br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>
3. 捕获UIButton事件，实现你自己的逻辑。比如，在我的程序中，当用户按下！键后，我会将！输入到UITextView中：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>keypressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; textView.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textView.text stringByAppendingString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;!&quot;</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>
这里是<a href="http://www.iphone-geek.cn/wp-content/uploads/2009/11/CustomizedKeypad.zip">源代码</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%ae%9a%e5%88%b6iphone%e9%94%ae%e7%9b%98/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>使用定制单元格创建与众不同的表格</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bd%bf%e7%94%a8%e5%ae%9a%e5%88%b6%e5%8d%95%e5%85%83%e6%a0%bc%e5%88%9b%e5%bb%ba%e4%b8%8e%e4%bc%97%e4%b8%8d%e5%90%8c%e7%9a%84%e8%a1%a8%e6%a0%bc</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bd%bf%e7%94%a8%e5%ae%9a%e5%88%b6%e5%8d%95%e5%85%83%e6%a0%bc%e5%88%9b%e5%bb%ba%e4%b8%8e%e4%bc%97%e4%b8%8d%e5%90%8c%e7%9a%84%e8%a1%a8%e6%a0%bc#comments</comments>
		<pubDate>Thu, 17 Sep 2009 03:39:00 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://iphonegeek.zuesitech.com/%e7%bc%96%e7%a8%8b/%e4%bd%bf%e7%94%a8%e5%ae%9a%e5%88%b6%e5%8d%95%e5%85%83%e6%a0%bc%e5%88%9b%e5%bb%ba%e4%b8%8e%e4%bc%97%e4%b8%8d%e5%90%8c%e7%9a%84%e8%a1%a8%e6%a0%bc</guid>
		<description><![CDATA[有读者问到怎样才能为Bikini.com程序创立如下图所示的用户界面：
&#160;

&#160;
创建定制表格
&#160;
这一切实际上很简单，只不过是让UITableView透明，所以可以看到背景。而每个单元格UITableViewCell允许定制。
&#160;

在浏览代码之前，我们看一下我们将创建的示例程序的截图：


&#160;
包含了tableview的主视图控制的类定义如下：
1234@interface MainViewController : UIViewController &#60;UITableViewDelegate, UITableViewDataSource&#62; 
&#123;
&#160; &#160; UITableView *theTableView; 
&#125;
&#160;
请注意在这里我没有使用UITableViewController，而是使用了带有UITableView的UIViewController。另外请注意代码中的UITableViewDelegate和UITableViewDataSource。
&#160;
先看看我是怎样对viewconroller进行初始化的：
1234567891011121314151617181920212223242526- &#40;id&#41;init 
&#123;
&#160; &#160; if &#40;self = &#91;super init&#93;&#41; 
&#160; &#160; &#123;
&#160; &#160; &#160; &#160; self.view = &#91;&#91;&#91;UIView alloc&#93; initWithFrame:
&#160; &#160; &#160; &#160; &#160; &#160; &#91;&#91;UIScreen mainScreen&#93; applicationFrame&#93;&#93; autorelease&#93;; 
&#160; &#160; &#160; &#160; // Setup the background
&#160; &#160; &#160; &#160; UIImageView *background = &#91;&#91;UIImageView alloc&#93; [...]]]></description>
			<content:encoded><![CDATA[<p>有读者问到怎样才能为Bikini.com程序创立如下图所示的用户界面：</p>
<p>&nbsp;</p>
<p><img src="http://iphonedevelopertips.com/wp-content/uploads/2009/10/tablecell1.png" alt=""></p>
<p>&nbsp;</p>
<h3>创建定制表格</h3>
<p>&nbsp;</p>
<p>这一切实际上很简单，只不过是让<strong>UITableView</strong>透明，所以可以看到背景。而每个单元格<strong>UITableViewCell</strong>允许定制。</p>
<p>&nbsp;</p>
<p><span id="more-129"></span></p>
<p>在浏览代码之前，我们看一下我们将创建的示例程序的截图：</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/tablecell2.png" alt="" width="200" height="300">
</p>
<p>&nbsp;</p>
<p>包含了tableview的主视图控制的类定义如下：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> MainViewController <span style="color: #002200;">:</span> UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt; <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; UITableView <span style="color: #002200;">*</span>theTableView; <br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>请注意在这里我没有使用<strong>UITableViewController</strong>，而是使用了带有<strong>UITableView</strong>的<strong>UIViewController</strong>。另外请注意代码中的<strong>UITableViewDelegate</strong>和<strong>UITableViewDataSource</strong>。</p>
<p>&nbsp;</p>
<p>先看看我是怎样对viewconroller进行初始化的：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Setup the background</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIImageView <span style="color: #002200;">*</span>background <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;background.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>background<span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>background release<span style="color: #002200;">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Create table view &nbsp; &nbsp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; theTableView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableView alloc<span style="color: #002200;">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0, 11, 320, 460<span style="color: #002200;">&#41;</span> style<span style="color: #002200;">:</span> UITableViewStylePlain<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>theTableView setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>theTableView setDataSource<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// This should be set to work with the image height </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>theTableView setRowHeight<span style="color: #002200;">:</span>68<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Transparent, so we can see the background </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>theTableView setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>theTableView setSeparatorStyle<span style="color: #002200;">:</span>UITableViewCellSeparatorStyleNone<span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>theTableView setIndicatorStyle<span style="color: #002200;">:</span>UIScrollViewIndicatorStyleWhite<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>theTableView<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> self; <br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>我创建视图，增加背景图像并定义tableview。我将表格的行高设定为相应图像的高度，然后设定表格背景为透明。</p>
<p>另一段代码说明了我们是怎样提供构成表格的单元格的。代码如下：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <br />
&nbsp; &nbsp; &nbsp; &nbsp; cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span style="color: #400080;">NSIndexPath</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; CustomCell <span style="color: #002200;">*</span>cell<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CustomCell alloc<span style="color: #002200;">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; initWithFrame<span style="color: #002200;">:</span>CGRectZero reuseIdentifier<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Default to no selected style and not selected</span><br />
&nbsp; &nbsp; cell.selectionStyle <span style="color: #002200;">=</span> UITableViewCellSelectionStyleNone;<br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Set the image for the cell</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>cell setTheImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Arrows%d.png&quot;</span>,indexPath.row <span style="color: #002200;">+</span> 1<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; <br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> cell;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>注意我没有使用<strong>UITableViewCell</strong> 而是使用了<strong>CustomCell</strong> 类（我下面会说明）。另外需注意的是第9行，我将要显示的图像设置到单元格中（下面会详细说明）。</p>
<p>&nbsp;</p>
<h3>创建定制单元格</h3>
<p>&nbsp;</p>
<p>我们看看定制单元格的代码，它也并不复杂，只不过是一个<strong>UIImageView</strong>和在单元格中设置图像的方法。根据你用户界面的需要，你还可以包括一个标签或其他UI元件。</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt; </span><br />
<br />
<span style="color: #a61390;">@interface</span> CustomCell <span style="color: #002200;">:</span> UITableViewCell <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; UIImageView <span style="color: #002200;">*</span>image;<br />
<span style="color: #002200;">&#125;</span> <br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setTheImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>icon; &nbsp;<br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>CustomCell的实现只包括3个方法：初始化，为单元格设定图像以及设定所选行的alpha值:</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &quot;CustomCell.h&quot;</span><br />
<br />
<span style="color: #a61390;">@implementation</span> CustomCell<br />
<span style="color: #11740a; font-style: italic;">/*----------------------------------------------------------------------- * <br />
&nbsp;*------------------------------------------------------------------------*/</span><br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame reuseIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>reuseIdentifier <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>frame reuseIdentifier<span style="color: #002200;">:</span>reuseIdentifier<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Cells are transparent </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.contentView setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> self;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #11740a; font-style: italic;">/*-----------------------------------------------------------------------* <br />
&nbsp;*------------------------------------------------------------------------*/</span> <br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setTheImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> icon <br />
<span style="color: #002200;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Alloc and set the frame </span><br />
&nbsp; &nbsp; image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span>icon<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; image.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>0, 0, 286, 68<span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Add subview</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.contentView addSubview<span style="color: #002200;">:</span>image<span style="color: #002200;">&#93;</span>; <br />
<span style="color: #002200;">&#125;</span> <br />
<br />
<span style="color: #11740a; font-style: italic;">/*------------------------------------------------------------------------* <br />
&nbsp;*------------------------------------------------------------------------*/</span> <br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelected<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>selected animated<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated<br />
<span style="color: #002200;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super setSelected<span style="color: #002200;">:</span>selected animated<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>selected <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; image.alpha <span style="color: #002200;">=</span> .5; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; image.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;<br />
<span style="color: #002200;">&#125;</span> <br />
<br />
<span style="color: #11740a; font-style: italic;">/*------------------------------------------------------------------------* <br />
&nbsp;*------------------------------------------------------------------------*/</span> <br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <br />
<span style="color: #002200;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>image release<span style="color: #002200;">&#93;</span>; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>; <br />
<span style="color: #002200;">&#125;</span> <br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>这样我们就创建了一个简单的定制tableview。</p>
<p>&nbsp;</p>
<h3>下载Xcode项目</h3>
<p>&nbsp;</p>
<p>在<a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/TableAndCells.zip">这里</a>下载。</p>
<p>&nbsp;</p>
<h5>
原文来自：<a href="http://iPhoneDeveloperTips.com/user-interface/creating-unique-looking-tables-with-custom-cells.html" rel="bookmark" title="Permanent Link: Creating Unique Looking Tables with Custom Cells">Creating Unique Looking Tables with Custom Cells</a> 翻译：bagusflyer</h5>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bd%bf%e7%94%a8%e5%ae%9a%e5%88%b6%e5%8d%95%e5%85%83%e6%a0%bc%e5%88%9b%e5%bb%ba%e4%b8%8e%e4%bc%97%e4%b8%8d%e5%90%8c%e7%9a%84%e8%a1%a8%e6%a0%bc/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
