从IBAction以编程方式添加UIButton

nfo*_*gia 1 iphone uibutton

我试图在每次按下按钮时进行操作,它会向视图添加一个按钮.我没有尝试过任何工作.

如果有人能够清楚做什么,我们将不胜感激.

这是我的按钮添加按钮的最新尝试:

-(IBAction) addButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,10,50,50);
    button.tag = 1;

    [button addTarget:self 
               action:@selector(buttonTouched:) 
     forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
}
Run Code Online (Sandbox Code Playgroud)

小智 6

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

Try the above codes.
Run Code Online (Sandbox Code Playgroud)