Xcode 学习之路 Interface Builder使用技巧

移动开发 iOS
本文介绍的是Xcode 学习之路 Interface Builder使用技巧,在坛子里面逛,发现了这么一排呢好文章,与大家分享一下。

最近在看电驴上down下来的ipad开发视频教程。为加深记忆,特在此做笔记。

前两集视频主要讲的是UIAlertView(相当于windows里面的MessageBox)

在interface builder里面添加了控件之后,要想使得控件响应事件必须做如下处理:

1:在XXXViewController.h里面添加事件响应函数声明如:

  1.   -(IBAction)btnOnclick:(id)sender; 

2:在相应的.m文件中实现这个函数

3:回到interface builder中鼠标右键从需要使用这个事件相应函数的控件拖拽到File's Owner上并在弹出的选项中选中步骤1中的函数名,使之关联起来。

Xcode 学习之路 Interface Builder使用技巧

注意:这个视频里面提到了一个特别的控件就是UIAlertView,因为这个控件没有在interface builder中提供出来编辑。所以如果这个要使这个控件的事件得到相应需要在XXXViewController.h文件里面声明类的时候使用协议(类似C++里面的抽象基类的多重继承)。样子如下:

  1. #import <UIKit/UIKit.h> 
  2. @interface AlertViewTestViewController : UIViewController <UIAlertViewDelegate>{  
  3. }  
  4. @end 

然后在实现文件里面实现对应的事件处理函数。对UIAlertView来说。要使用的协议是UIAlertViewDelegate,要实现的事件响应函数是 - (void)alertView:(UIAlertView* )alertView clickedButtonAtIndex:(NSInteger)buttonIndex。实际上,使用了协议之后,可以根据协议名字在帮助里面很快的找到这个协议需要实现哪些函数。

控件还有另外一个比较重要的东西就是数据绑定。在xcode里面要实现这么一个东西步骤基本和上面的类似

1:在XXXViewController.h文件里面声明之,下面声明了一个文本数据

  1. @interface OutletAndActionViewController : UIViewController {  
  2.     IBOutlet UITextField *txtName;  
  3. }  
  4. @property(nonatomic, retain) UITextField *txtName;  
  5. @end 

2:实现文件里面给出set和get函数。如果使用了属性,你懂的。。。

  1. @synthesize txtName; 

3:回到interface builder中鼠标右键从File's Owner中拖拽到需要绑定这个数据的控件上并在弹出的选项中选中,使之关联起来。拖拽方向和上面相反。写到这里。我在想IBAction和IBOutlet到底是个啥?

Xcode 学习之路 Interface Builder使用技巧

在头文件中找到了他们的定义:

  1. #ifndef IBOutlet   
  2. #define IBOutlet   
  3. #endif  
  4.  
  5. #ifndef IBAction   
  6. #define IBAction void   
  7. #endif 

几乎啥都没干。最后在cocoachina上找到了答案。

原帖:http://www.cocoachina.com/bbs/read.php?tid-18829.html

内容如下:

These two keywords do absolutely nothing as far as the compiler is concerned. IBOutlet gets entirely removed from the code before the compiler ever sees it. IBAction resolves to a void return type, which just means that action methods do not return a value. So, what’s going on here?
The answer is simple, really: IBOutlet and IBAction are not used by the compiler. They are used by Interface Builder. Interface Builder uses these keywords to parse out the outlets and actions available to it. Interface Builder can only see methods that are prefaced with IBAction and can only see variables or properties that are prefaced with IBOutlet. Also, the presence of these keywords tells other programmers, looking at your code in the future, that the variables and methods in question aren’t dealt with entirely in code. They’ll need to delve into the relevant nib file to see how things are hooked up and used.

敢情这两关键字完全是给interface builder看的。

小结:Xcode 学习之路 Interface Builder使用技巧 的内容介绍完了希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-03 14:13:45

Xcode 4 Interface

2011-07-20 09:49:41

Xcode Interface Builder

2011-07-28 13:47:20

Xcode Interface

2011-07-06 15:06:46

Xcode Cocoa

2011-08-05 10:01:23

Xcode Interface

2011-07-06 15:14:34

iOS Xcode

2011-07-25 10:30:41

Objective-C Xcode 重构

2011-07-25 11:02:29

Objective-C Xcode 标签

2011-07-25 10:14:13

Objective-C Xcode

2011-08-11 16:31:08

XCode

2011-08-05 09:38:46

Interface B Cocoa 界面

2011-08-19 15:16:41

XCodeUserScripts脚本

2011-07-20 14:31:56

XCode User Scrip 脚本

2011-08-05 09:48:46

iPhone Interface

2011-07-22 15:56:18

iPhone Interface Builder

2011-08-08 17:05:02

XCode UserScript 脚本

2011-07-26 17:47:13

2013-04-18 10:19:40

iOS开发Xcode调试

2010-01-11 16:19:05

C++ Builder

2011-08-04 18:09:32

Xcode 技巧 文档
点赞
收藏

51CTO技术栈公众号