![](/img/trans.png)
[英]I lose access to my IBOutlets when making view controller new detail view controller
[英]I have IBOutlets for my game view controller UIButtons that need to be organized better
我的UIButtons
有IBOutlets
,如下所示。 這將持續30次。 我想知道是否有一種方法可以使我不必像這樣列出每個按鈕插座並使它更有條理?
如果有人能指出正確的方向,我將深表感謝。
@property (weak,nonatomic) IBOutlet UIButton *level1Button;
@property (weak,nonatomic) IBOutlet UIButton *level2Button;
@property (weak,nonatomic) IBOutlet UIButton *level3Button;
您可以使用插座集合來代替很多插座。 在您的.h文件中:
@property (weak, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
然后,您可以控制按鈕到該行的拖動,它們將按照拖動的順序都在數組中。
另一個選項是(帶有6個UIButtons
示例):
創建按鈕。 例如在viewDidLoad
:
//This is an array with your buttons positions
self.cgPointsArray = @[[NSValue valueWithCGPoint:CGPointMake(25, 130)],[NSValue valueWithCGPoint: CGPointMake(70, 130)],
[NSValue valueWithCGPoint:CGPointMake(115, 130)],[NSValue valueWithCGPoint:CGPointMake(160, 130)],
[NSValue valueWithCGPoint:CGPointMake(205, 130)],[NSValue valueWithCGPoint:CGPointMake(250, 130)]];
// for loop to allocate the buttons
for (int i = 0; i < [self.cgPointsArray count]; i++)
{
NSValue *value = [self.cgPointsArray objectAtIndex:i];
CGPoint point = [value CGPointValue];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(point.x, point.y, 20, 20);
[button setBackgroundColor:[UIColor blueColor]];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
}
然后,您可以通過以下tags
來管理事件:
- (void) buttonClicked: (id) sender
{
UIButton *tempButton = (UIButton*) sender;
int tag = tempButton.tag;
if (tag == 0)
{
//Do something
}
else if (tag == 1)
{
//Do something
}
else if (tag == 2)
{
//Do something
}
else if (tag == 3)
{
//Do something
}
else if (tag == 4)
{
//Do something
}
else if (tag == 5)
{
//Do something
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.