MyButton *tempButton = [[MyButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
tempButton.backgroundColor = [UIColor redColor];
[tempButton addTarget:self action:@selector(mybuttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tempButton];
来源: