3

ターゲットのセレクターを呼び出さないボタンがあります。

クリックすると、強調表示されます。しかし、ブレークポイントを に設定しましたplayButtonClickedが、到達することはありません。

リリースされるかどうかはわかりませんが、そうではないと思います。ARC を有効にしていますがretain、 またはを呼び出すことができませんrelease

また、明示的に有効にしようとしましuserInteractionEnabledたが、それでも違いはありません。

これが私のコードです:

#import "MainMenuView.h"
@implementation MainMenuView
- (void)initializeButton:(UIButton*)button withText:(NSString*)text buttonHeight:   (int)buttonHeight buttonWidth:(int)buttonWidth buttonYInitialPosition:(int)buttonYInitialPosition buttonXPosition:(int)buttonXPosition
{
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(buttonXPosition, buttonYInitialPosition, buttonWidth, buttonHeight);

    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];

    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:24];
    [self addSubview:button];
    [self bringSubviewToFront:button];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {        
        self.backgroundColor = [UIColor yellowColor];

        UIImage *backgroundImage = [UIImage imageNamed:@"title_background.jpeg"];
        UIImageView *background = [[UIImageView alloc] initWithFrame:frame];
        background.image = backgroundImage;
        background.backgroundColor = [UIColor greenColor];
        [self addSubview:background];

        int centerWidth = frame.size.width / 2;
        int centerHeight = frame.size.height / 9;
        int centerXPos = frame.size.width / 4;
        int buttonYInitialPosition = frame.size.height / 2 + frame.size.height / 20;
        int buttonYOffset = frame.size.height / 7;

        // init buttons
        [self initializeButton:playButton withText:@"Play" buttonHeight:centerHeight buttonWidth: centerWidth
        buttonYInitialPosition:buttonYInitialPosition buttonXPosition:centerXPos];
        [playButton addTarget:self action:@selector(playButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void) playButtonClicked:(id)sender
{
    NSLog(@"Play Button Clicked");
}
@end
4

4 に答える 4

1

メソッドで初期化を行う必要があると思います:

-(void)awakeFromNib {}

すべての nib オブジェクトがロードされ、準備が整っているという点で、-viewDidLoadカスタムの方法に似ています。UIViews

于 2013-09-07T21:13:14.583 に答える