在图形界面编程时,解决的第一问题就是如何将静态界面与代码关联起来,或者说是代码如何与界面上的对象
通信, 代码如何操作界面上的对象。在iPhone平台上,引入了IBOutlet与IBAction。通过在变量前增加IBOutlet
来说明该变量将与界面上的某个UI对象对应,在方法前增加IBAction来说明该方法将与界面上的事件对应.
<wbr></wbr>
下面通过一个连接网络服务器(NetworkConnection)的例子来说明IBOutlet与IBAction。
界面上有host 与 port 的Text Field UI对象,一个Button对象。
所以代码中需要定义两个IBOutlet变量,分别用来定义host与port; 一个IBAction方法,用来发起连接动作。
<wbr></wbr>
在NetworkConnectionViewCon<wbr><span style="font-size:12pt">troller.h文件中:</span></wbr>
定义变量:
@interface NetworkConnectionViewCon<wbr><span style="font-size:12pt">troller : UIViewController {</span><br style="line-height:10px"><wbr><span style="font-family:Arial"><span style="color:rgb(51,102,255)"><wbr><wbr></wbr></wbr></span><span style="color:rgb(51,102,255); font-size:12pt">UITextField *host;</span><br style="line-height:10px; color:rgb(51,102,255)"><span style="color:rgb(51,102,255)"><wbr><wbr><wbr></wbr></wbr></wbr></span><span style="color:rgb(51,102,255); font-size:12pt">UITextField *port;</span><br style="line-height:10px"><span style="font-size:12pt">}</span><br style="line-height:10px"><br style="line-height:10px"><span style="font-size:12pt">将这两个变量说明为IBOutlet变量:</span></span></wbr></wbr>
@property(nonatomic, retain)<wbr></wbr>IBOutlet<wbr></wbr>UITextField *host;
@property(nonatomic, retain)<wbr></wbr>IBOutlet UITextField *port;
<wbr></wbr>
在NetworkConnectionViewCon<wbr><span style="font-size:12pt">troller.m文件中增加:</span></wbr>
@synthesize host;
@synthesize port;
打开NetworkConnectionViewCon<wbr><span style="font-size:12pt">troller.xib文件,拖两个Text Field对象到上面。</span></wbr>
按住Ctrl键,拖拽File's Owner到Text Field之上,会弹出Outlets选择列表,在列表中可以看到host与port。
分别为两个Text Field选择Outlet变量。这样做了以后,界面上的Text Field对象就与程序中定义的变量就关联起来,
当改变变量的属性时,就会显现在界面上。
为了检验变量是否与界面对象关联,在viewDidLoad方法中给变量付值然后编译运行。
- (void)viewDidLoad
{
<wbr><wbr><wbr><span style="font-size:12pt">[super viewDidLoad];</span><br style="line-height:10px"><wbr><wbr><span style="font-family:Arial"><span style="color:rgb(51,102,255)"><wbr></wbr></span><span style="color:rgb(51,102,255); font-size:12pt">host.text = @"192.168.1.100";<br style="line-height:10px"><wbr><wbr><wbr></wbr></wbr></wbr></span><span style="color:rgb(51,102,255); font-size:12pt">port.text = @"8080";</span><br style="line-height:10px"><span style="font-size:12pt">}</span><br style="line-height:10px"></span></wbr></wbr></wbr></wbr></wbr>
运行后,可以在界面的Text Field中看到这些值,说明变量与界面对象关联正确。从而就可以在界面中看到变量的值。
<wbr></wbr>
在NetworkConnectionViewCon<wbr><span style="font-size:12pt">troller.h文件中增加一个IBAction方法:</span></wbr>
-(IBAction)connectNetwork;
在NetworkConnectionViewCon<wbr><span style="font-size:12pt">troller.m文件中实现该方法:</span></wbr>
-(IBAction)connectNetwork
{
<wbr><wbr><span style="font-size:12pt">UIAlertView *alter = [[UIAlertView alloc] initWithTitle: @"Connection Network" message: @"sending command to the server" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];</span><br style="line-height:10px"><wbr><wbr><wbr><br style="line-height:10px"><wbr><wbr><wbr><span style="font-size:12pt">[alter show];</span><br style="line-height:10px"><wbr><wbr><wbr><span style="font-size:12pt">[alter release];</span><br style="line-height:10px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
//connect network<wbr></wbr>
//............
}
打开NetworkConnectionViewCon<wbr><span style="font-size:12pt">troller.xib,拖一个Round Rect Button到上面。</span><br style="line-height:10px"><span style="font-size:12pt">然后按住Ctrl键,拖拽该button到File's Owner上,在弹出的IBAction列表中</span></wbr>
选择connectNetwork。这样当该button被按下弹起后就会调用connectNetwork方法。
IBOutlet与IBAction是iPhone应用开发的基础,是成功迈向iPhone平台应用开发的第一步。
本文来自:http://kb.cnblogs.com/a/2337431/