0

メソッド内に次のように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呼び出しが実行されていることを確認しました。

どんなポインタもありがたいことに受け取りました!トニー。

4

1 に答える 1

2

これは、メソッドが常に単一のインスタンスを使用するためです。初期化の最初の行を見てください

myButton = [UIButton buttonWithType:UIButtonTypeCustom];

この行を次のように置き換えます

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

毎回ボタンの新しいインスタンスを返します。

次のようなボタンインスタンスを宣言しました。

IBOutlet UIButton *btn1, *btn2, *btn3;

これまでと同じように、メソッドを使用して新しいボタンを作成します。そのボタンを、尊重されるボタンオブジェクトに割り当てるだけです。

btn1 = [self setupButton:originalButton withSelector:@selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"originalreg.png" withHighlightImage:@"originalregblue.png"];

btn2 = [self setupButton:originalButton withSelector:@selector(duplicateButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"duplicatereg.png" withHighlightImage:@"originalregblue.png"];

btn3 = [self setupButton:originalButton withSelector:@selector(olderButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"olderreg.png" withHighlightImage:@"originalregblue.png"];
于 2013-03-24T16:35:45.093 に答える