在我的.h中
IBOutlet NSMutableArray *buttons;
@property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *buttons;
-(void)play:(UIButton *)theButton;在我的.m中
-(void)initButtons{
buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIButton *myBut = [UIButton alloc];
[buttons addObject: myBut];
[[buttons objectAtIndex:0] addtarget:self action@selector(play:) forControlEventTouchUpInside];
}..。
-(void)dealloc{
[buttons dealloc];
[super deallloc];
}.
-(void)viewDidLoad{
[super viewDidLoad];
[self initButtons];
}我将界面生成器中的按钮IBoutletCollection拖到了一个简单的按钮上,但当我测试它时,它并没有执行预期的操作;
我需要指出的是,如果我将我的操作转换为(IBAction)而不是(void),并将其链接到按钮,它就会工作;
我不太了解NSArrays和outlet集合。
发布于 2012-02-06 20:12:02
使用连接到NIB中的集合的任何按钮为您设置数组。它无法执行任何操作,因为您已经在此处重置了ivar:
buttons = [[NSMutableArray alloc] initWithCapacity:1];…或者因为您尚未将按钮连接到集合。
发布于 2012-02-06 20:03:51
在.h file.connect中声明你的UIButton myBut为带有属性的IBOutlet,你在xib中的按钮出口到myBut.No需要声明NSMutableArray为IBOutletCollection或IBOutlet.You,只需声明它,并且不需要在initButtons方法中再次分配myBut。
你可以这样做。
viewController.h
@property (strong, nonatomic) IBOutlet UIButton *myButton;
@property (nonatomic,strong) NSMutableArray *buttons;
-(void)initButtons;
-(void)play:(id)sender;inSide xib将您的按钮插座连接到myButton
viewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self initButtons];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)initButtons{
buttons = [[NSMutableArray alloc] initWithCapacity:1];
[buttons addObject: myButton];
[[buttons objectAtIndex:0] addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)play:(id)sender
{
NSLog(@"button tapped");
}发布于 2012-02-06 20:11:42
你不需要插座来连接按钮和方法。
去掉你所有的outlet、property和initButtons代码,只需要这样:
//in .h
-(IBAction)play:(UIButton *)theButton;
//in .m
-(IBAction)play:(UIButton *)theButton
{
//the code for your play action
}然后在Interface Builder中,按住ctrl键并从按钮拖动到文件的所有者,然后选择play: action。
https://stackoverflow.com/questions/9159567
复制相似问题