IBAction IBOutlet IBOutletCollection

来源:互联网 发布:金山打字软件 编辑:程序博客网 时间:2024/06/07 06:30

IBAction IBOutlet IBOutletCollection  


#define IBAction void
#define IBOutlet
#define IBOutletCollection(ClassName)
Constants


IBAction


Type qualifier used by Interface Builder to synchronize actions. Use this type as the return type of any action methods defined in your project. For examples of how to use this identifier, see “Xcode Integration”.
Available in iOS 2.0 and later.
Declared in UINibDeclarations.h.


IBOutlet


Identifier used to identify a property so Interface Builder can synchronize the display and connection of outlets with Xcode. Insert this identifier immediately before the type in any declarations. For examples, including how to use it with the @property syntax, see “Xcode Integration”.
Available in iOS 2.0 and later.
Declared in UINibDeclarations.h.




IBOutletCollection




Identifier used to qualify a one-to-many instance-variable declaration so that Interface Builder can synchronize the display and connection of outlets with Xcode. You can insert this macro only in front of variables typed as NSArray or NSMutableArray.
This macro takes an optional ClassName parameter. If specified, Interface Builder requires all objects added to the array to be instances of that class. For example, to define a property that stores only UIView objects, you could use a declaration similar to the following:
@property (nonatomic, retain) IBOutletCollection(UIView) NSArray *views;
For additional examples of how to declare outlets, including how to create outlets with the @property syntax, see “Xcode Integration”.
Available in iOS 4.0 and later.
Declared in UINibDeclarations.h.


下面的不是原文翻译,而是解释:
和以往的略微有些不同,由于我们有两个View,但是这两个View中的按钮的作用是一样的,所以我们在创建按钮的Outlet时,可以使用Outlet 集合,也就是Outlet Collection,Outlet Collection和Outlet的区别是,Outlet只能对应一个控件,Outlet Collection则可以对应多个控件,其实Outlet Collection就是一个Outlet的数组,里面可以存放任意多个Outlet,然后对其一一进行遍历。有了Outlet Collection后,我们在写Action的时候,只需要便利Outlet Collection,就可以其中包含的每个控件进行操作,会方便很多(否则你需要对每个控件声明一个Outlet,然后一一操作,这个不仅增加代码的复 杂度,而且还很容易遗漏控件)



示例代码如下:





#import <UIKit/UIKit.h>


@interface IBOutletCollectionTestViewController : UIViewController
{
    
}


@property(nonatomic,retain) IBOutletCollection(UIButton) NSArray* buttonList;


@end



//
//  IBOutletCollectionTestViewController.m
//  IBOutletCollectionTest
//
//  Created by lv wei on 13-8-29.
//  Copyright (c) 2013年 lv wei. All rights reserved.
//


#import "IBOutletCollectionTestViewController.h"


@interface IBOutletCollectionTestViewController ()


@end


@implementation IBOutletCollectionTestViewController
@synthesize buttonList = _buttonList;


- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
    for (UIButton* button in _buttonList) {
        NSLog(@"button = %@",button.titleLabel.text);
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dealloc
{
    [_buttonList release];
    
    [super dealloc];
}


@end
在IB中,添加四个UIButton,并连接

 
日志输出

2013-08-29 15:13:56.186 IBOutletCollectionTest[8178:11303] button = Button1
2013-08-29 15:13:56.188 IBOutletCollectionTest[8178:11303] button = Button2
2013-08-29 15:13:56.189 IBOutletCollectionTest[8178:11303] button = Button3
2013-08-29 15:13:56.191 IBOutletCollectionTest[8178:11303] button = Button4