我有一個類CustomView
(UIView
的子類沒有xib文件),我在其中創建一些標籤和按鈕。我想在我的另一個UIViewController
中使用這個類來添加這些標籤和按鈕。我能夠使用自定義視圖添加標籤和按鈕到我的viewController,但如果我添加一些操作或事件到按鈕(這是在自定義視圖中),它不起作用。請建議我應該如何爲按鈕添加操作。在UIView的子類中添加一個按鈕和動作
//ViewController code
CustomView *slider=[[CustomView alloc]init];
[self.view addSubview:slider];
//CustomView code
toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleButton setTitle:@"" forState:UIControlStateNormal];
toggleButton.userInteractionEnabled=YES;
// add drag listener
[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
// center and size
toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, width, frame.size.height);
toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.0 alpha:0.1];
[toggleButton.layer setBorderWidth:4.0];
[toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
toggleButton.layer.cornerRadius=4.0;
[toggleButton setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
// add it, centered
[self addSubview:toggleButton];
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
NSLog(@"inside drag");
}
這是相關的則hitTest? – user1787741 2013-02-21 08:40:37