メソッド内に次のように10個ほどのボタンを設定しています。
@implementation MyViewController
UIButton *originalButton;
etc...
- (void)setupButtons
{
originalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[originalButton addTarget:self action:@selector(originalButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
originalButton.frame = CGRectMake(20.0, 30.0, 100.0, 39.0);
UIImage *buttonImage = [[UIImage imageNamed:@"originalreg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:@"originalregblue.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[originalButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[originalButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[self.view addSubview:originalButton];
etc…
}
効率を上げるために、共通のコードを別の方法に組み込むことにしました。
- (void)setupButton:(UIButton *)myButton withSelector:(SEL)selector withX:(CGFloat)x withY:(CGFloat)y withRegImage:(NSString *)regImage withHighlightImage:(NSString *)highlightImage
{
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(x, y, 100.0, 39.0);
UIImage *buttonImage = [[UIImage imageNamed:regImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:highlightImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[myButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
}
…そしてそれをそのように呼びます:
- (void)setupButtons
{
[self setupButton:originalButton withSelector:@selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"originalreg.png" withHighlightImage:@"originalregblue.png"];
etc...
}
これはすべて機能しますが、私のボタンの1つが他のすべてを非表示にするために使用されます。元の設定では、[ボタンを非表示]ボタンを押すと、他のボタンが非表示になりました。現在、それらは画面に残ります。そのためのコードは次のとおりです。
[self setupButton:hideButtonsButton withSelector:@selector(hideButtonsButtonWasPressed:) withX:20.0 withY:530.0 withRegImage:@"hidebuttonsreg.png" withHighlightImage:@"hidebuttonsregblue.png"];
- (void)hideButtonsButtonWasPressed:(id)sender
{
// hide the buttons
originalButton.hidden = YES;
originalButton.enabled = NO;
etc…
}
このメソッドが呼び出され、setHidden/setEnabled
呼び出しが実行されていることを確認しました。
どんなポインタもありがたいことに受け取りました!トニー。