0

簡単なプロジェクトを実行するために、ios 7 で XCode 5 を使用しています。プログラムで作成されたボタンを押すと、次のエラーが表示されます。

2013-11-10 09:16:02.969 Project2[446:70b] +[KNSunDynamicUIButton buttonSavePressed:]: unrecognized selector sent to class 0x221424

詳細:
UIButton オブジェクトをプログラムで作成するために使用するカスタム メソッドを作成します。そのカスタム メソッドは、任意のビュー コントローラーで使用されます。そのカスタム メソッドには、x、y、幅、高さ、ボタン タイトル、およびイベント ハンドラーであり、" (SEL)selector "のように渡されるセレクターなどの渡されたパラメーターが必要です。

カスタム メソッド(ヘルパー クラスに属します):

+(UIButton*)kNSunSetupWithSelector:(SEL)selector    withX:(int)x y:(int)y width:(int)width height:(int)height
                       buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{

    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setTitle:@"Save" forState:UIControlStateNormal];
    button.frame = CGRectMake(x, y, width, height);
    button.backgroundColor = [UIColor greenColor];

// selector is used over here
    [button addTarget:self
                    action:selector
          forControlEvents:UIControlEventTouchDown];

    return button;
}

そして、View Controller .m fileで、そのカスタム メソッドを次のように呼び出します。

-(void)setUpButtons
{
            SEL selector = @selector(buttonSavePressed:);
            _buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector withX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:(_buttonSaveColor)];

            [self.view addSubview:_buttonSave];

}

    @interface MyViewController ()
    {
    ...

        // buttons
        UIButton* _buttonSave;
    }
    @end

そして、同じView Controller .mファイル内のボタンのイベントハンドラーの定義は次のようになります:

- (void)buttonSavePressed:(UIButton*)button
{
    NSLog(@"Button Save clicked.");
}

コードを実行してボタンを押すと、上記の例外が表示されます。助けてくださいよろしくお願いします。

PS 署名に「(SEL)selector」パラメーターを持たない代替手段としてカスタムメソッドを書き直し、そのカスタムメソッドを呼び出すコントローラービューにセレクターのコーディングの仕事をさせる場合、例外はありません:

-(void)setUpButtons
{
    //Note: the codes are in my view controller .m file

_buttonSave = [KNSunDynamicUIButton kNSunSetupWithX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:_buttonSaveColor];


   // Note: selector coding is taken care by codes of my view controller instead of by custom method
   [_buttonSave addTarget:self
    action:@selector(buttonSavePressed:)
    forControlEvents:UIControlEventTouchDown];



    [self.view addSubview:_buttonSave];
    [_buttonSave setupView];
}

そして、別のカスタムメソッド (動的に渡されたセレクターを処理しないため、このメソッドは好きではありません):

+(UIButton*)kNSunSetupWithX:(int)x y:(int)y width:(int)width height:(int)height
                   buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setTitle:@"Save" forState:UIControlStateNormal];
    button.frame = CGRectMake(x, y, width, height);
    button.backgroundColor = [UIColor greenColor];

    return button;
}
4

1 に答える 1

4

問題はヘルパークラスの行です:

[button addTarget:self action:selector forControlEvents:UIControlEventTouchDown];

ここで、ヘルパークラスには、セレクターを通過するメソッドが含まれると言いました。この場合は、buttonSavePressed:メソッドです。

ヘルパー メソッドに inController パラメーターを追加して、どのコントローラーにセレクター メソッドが配置されているかを伝える必要があります。

特定のケースでは、ヘルパー メソッドは次のようになります (セレクター パラメーターの直後にinControllerパラメーターが追加されることに注意してください)。

+(UIButton*)kNSunSetupWithSelector:(SEL)selector
                      inController:(UIViewController*)controller
                             withX:(int)x
                                 y:(int)y
                             width:(int)width
                            height:(int)height
                       buttonTitle:(NSString*)buttonTitle
                   backGroundColor:(CGColorRef) backGroundColor
{

UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button setTitle:@"Save" forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = [UIColor greenColor];

// selector is used over here
[button addTarget:controller
           action:selector
 forControlEvents:UIControlEventTouchDown];

    return button;
}

コントローラーから次のように呼び出す必要があります。

-(void) setupButtons
{
    SEL selector = @selector(buttonSavePressed:);
    _buttonSaveColor = [UIColor orangeColor].CGColor;
    _buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector
                                                  inController:self
                                                         withX:470
                                                             y:410
                                                         width:160
                                                        height:40
                                                   buttonTitle:@"Save"
                                               backGroundColor:(_buttonSaveColor)];

    [self.view addSubview:_buttonSave];
}
于 2013-11-10T16:45:24.190 に答える