2011-05-22 75 views

回答

0

您可以简单地添加这样的:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

[button setTitle:@"Button title" forState:UIControlStateNormal]; //Set the title for regular state 
[button addTarget:self 
     action:@selector(yourMethod:) 
forControlEvents:UIControlEventTouchDown]; 
button.frame = CGRectMake(40, 100, 160, 240); //make the frame 
[view addSubview:button]; //add to current view 

目前已经有几个线程覆盖这个话题,虽然,请使用搜索栏。 :)

+0

顺便说一句:“yourAction”需要是一个IBAction:(id)sender .. – JulenissensHjelper 2011-05-22 21:31:16

+0

不是在iOS上它不。在iOS上,IBActions可以使用0,1或2个参数。 (第二个是UIEvent) – 2013-06-05 05:55:15

0
//required ... 
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; 
[button setTitle:@"who am i?" forState:UIControlStateNormal]; 
[button setFrame:CGRectMake(20, 20, 200, 40)]; 
[button addTarget:self action:@selector(performAction) forControlEvents:UIControlEventTouchUpInside]; 

//optional properties.. 
[button setBackgroundColor:[UIColor greenColor]]; 
[button setImage:[UIImage imageNamed:@"someImg.png"] forState:UIControlStateNormal]; 
[button setBackgroundImage:[UIImage imageNamed:@"green-hdr-bg.png"] forState:UIControlStateNormal]; 
[button setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; 

[yourCustomView addSubview:button]; 



-(void)performAction 
{ 
    //do you button click task here 
} 
1

任何视图都可以有子视图,即使视图实现了drawRect:。子视图的内容总是覆盖超级视图的内容。而UIButton是一个视图。因此,只需将您的按钮添加为您的自定义视图的子视图。

相关问题