IBOutletCollection(UI按钮)

发布于 2025-01-03 01:13:37 字数 828 浏览 0 评论 0原文

在我的.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无语# 2025-01-10 01:13:37

该数组是通过您连接到 NIB 中的集合的任何按钮来设置的。它无法执行任何操作,因为您已在此处重置了 ivar:

buttons = [[NSMutableArray alloc] initWithCapacity:1];

...或者因为您尚未将按钮连接到集合。

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:

buttons = [[NSMutableArray alloc] initWithCapacity:1];

…or because you've not connected buttons to the collection.

会发光的星星闪亮亮i 2025-01-10 01:13:37

您不需要插座将按钮连接到方法。

摆脱所有的插座和属性以及 initButtons 代码,只需执行以下操作:

//in .h

-(IBAction)play:(UIButton *)theButton;

//in .m

-(IBAction)play:(UIButton *)theButton
{
    //the code for your play action
}

然后在 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:

//in .h

-(IBAction)play:(UIButton *)theButton;

//in .m

-(IBAction)play:(UIButton *)theButton
{
    //the code for your play action
}

Then in Interface Builder, ctrl-drag from the button to the File's Owner and select the play: action.

秋意浓 2025-01-10 01:13:37

将您的 UIButton myBut 声明为具有 .h 文件中的属性的 IBOutlet。将 xib 中的按钮出口连接到 myBut。无需将 NSMutableArray 声明为 IBOutletCollection 或 IBOutlet。您只需声明它,无需在 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");
}

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

@property (strong, nonatomic) IBOutlet UIButton *myButton;
@property (nonatomic,strong) NSMutableArray *buttons;
-(void)initButtons;
-(void)play:(id)sender;

inSide xib connect your button outlet to 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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文