xcode IBOutletCollection (UIbutton)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9159567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:13:54  来源:igfitidea点击:

IBOutletCollection (UIbutton)

objective-cxcodeiboutlet

提问by Vlad Otrocol

in my .h I have

在我的 .h 我有

IBOutlet NSMutableArray *buttons;
@property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *buttons;
-(void)play:(UIButton *)theButton;

in my .m I have

在我的 .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];
}

I dragged the buttons IBoutletCollection in interface builder to a simple button, but when I test it it doesn't perform the expected action;

我将界面构建器中的按钮 IBoutletCollection 拖到一个简单的按钮上,但是当我测试它时,它没有执行预期的操作;

I should mention that if I turn my action into (IBAction) instead of (void) and link it to the button it works;

我应该提到的是,如果我将我的操作转换为 (IBAction) 而不是 (void) 并将其链接到它起作用的按钮;

I don't understand very well NSArrays and outlet collections.

我不太了解 NSArrays 和 outlet 集合。

回答by justin

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:

使用您已连接到 NIB 中的集合的任何按钮为您设置阵列。它无法执行任何操作,因为您已在此处重置 ivar:

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

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

……或者因为您没有将按钮连接到集合。

回答by Mudit Bajpai

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.

将您的 UIButton myBut 声明为 .h 文件中具有属性的 IBOutlet。将您在 xib 中的按钮出口连接到 myBut。无需将 NSMutableArray 声明为 IBOutletCollection 或 IBOutlet。您只需声明它,无需在 initButtons 方法中再次分配 myBut。

You can do like this.

你可以这样做。

viewController.h

视图控制器.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

inSide xib 将您的按钮插座连接到 myButton

viewController.m

视图控制器.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");
}

回答by Nick Lockwood

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:

去掉你所有的插座和属性以及 initButtons 代码,只需要这个:

//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.

然后在 Interface Builder 中,按住 ctrl 从按钮拖动到 File's Owner 并选择 play: 动作。