【发布时间】:2011-09-19 13:05:00
【问题描述】:
在我的简单程序中,按钮有问题。按钮是视图的子视图,视图是 myViewController.view 的子视图。
此按钮是动态创建的,但它的操作不起作用。如何发送事件?
这是我的代码。
//视图控制器
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
bookCount = 0;
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[self.view addSubview:subview];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame: subview.bounds];
[btn addTarget:self action:@selector(onBtnchild:) forControlEvents:UIControlEventTouchUpInside];
[subview addSubview:btn];
.....
}
return self;
}
-(void) onBtnchild:(id) sender{
NSLog(@"HI");
}
我可以看到我的按钮,但我无法点击。你有什么想法请教我。
【问题讨论】:
标签: objective-c ios uibutton subview viewcontroller