IBOutletCollection(UI按钮)
在我的.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];
}
我将界面生成器中的按钮IBoutletCollection拖动到一个
-(void)viewDidLoad{
[super viewDidLoad];
[self initButtons];
}
简单的按钮,但是当我测试它时它没有执行预期的操作;
我应该提到,如果我将我的操作变成 (IBAction) 而不是 (void) 并将其链接到它起作用的按钮;
我不太了解 NSArrays 和插座集合。
in my .h I have
IBOutlet NSMutableArray *buttons;
@property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *buttons;
-(void)play:(UIButton *)theButton;
in my .m I have
-(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];
}
I dragged the buttons IBoutletCollection in interface builder to a simple button, but when I test it it doesn't perform the expected action;
I should mention that if I turn my action into (IBAction) instead of (void) and link it to the button it works;
I don't understand very well NSArrays and outlet collections.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该数组是通过您连接到 NIB 中的集合的任何按钮来设置的。它无法执行任何操作,因为您已在此处重置了 ivar:
...或者因为您尚未将按钮连接到集合。
The array is set for you with whatever buttons you've connected to the collection in the NIB. It fails to do anything because you've reset the ivar here:
…or because you've not connected buttons to the collection.
您不需要插座将按钮连接到方法。
摆脱所有的插座和属性以及 initButtons 代码,只需执行以下操作:
然后在 Interface Builder 中,按住 Ctrl 键并从按钮拖动到文件所有者,然后选择 play: 操作。
You don't need outlets to connect buttons to methods.
Get rid of all your outlet and property and initButtons code and just have this:
Then in Interface Builder, ctrl-drag from the button to the File's Owner and select the play: action.
将您的 UIButton myBut 声明为具有 .h 文件中的属性的 IBOutlet。将 xib 中的按钮出口连接到 myBut。无需将 NSMutableArray 声明为 IBOutletCollection 或 IBOutlet。您只需声明它,无需在 initButtons 方法中再次分配 myBut。
你可以这样做。
viewController.h
inSide xib 将您的按钮插座连接到 myButton
viewController.m
Declare your UIButton myBut as an IBOutlet with property in .h file.connect your button outlet in xib to myBut.No need to declare NSMutableArray as an IBOutletCollection or IBOutlet.You simply declare it and No need to allocate myBut again inside initButtons method.
You can do like this.
viewController.h
inSide xib connect your button outlet to myButton
viewController.m