Kir*_*Koa 3 iphone objective-c uibutton ios
我有一个不调用其目标选择器的按钮.
当我点击它时,它会突出显示.但是,我设定了一个突破点,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
Run Code Online (Sandbox Code Playgroud)
您的代码没有按照您的想法执行.
当你传递playButton中-initializeButton:...,然后立即创建一个新的按钮,并将其分配给变量,你已不再是该值工作playButton指向.因此,当你-addTarget:action:forControlState:之后打电话时,你会为任何目标分配一个目标playButton指向的内容,这不是您刚刚创建和添加的按钮.
传递指针(默认情况下)按值完成,这意味着您只拥有指针所保存的地址,而不是指针本身的引用.因此,您无法更改指针本身,只能更改它指向的对象.如果要修改指向的内容,可以通过引用传递指针; 或者您可以重构代码,以便您始终直接对指针执行操作 - 例如,您可以使用ivar或属性并让初始化方法设置该属性.或者您可以返回按钮并将其分配给您的变量或属性.
| 归档时间: |
|
| 查看次数: |
2734 次 |
| 最近记录: |