How do you add an action to a button programmatically in xcode

后端 未结 10 1238
野的像风
野的像风 2020-11-29 21:40

I know how to add an IBAction to a button by dragging from the interface builder, but I want to add the action programmatically to save time and to avoid switching back and

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 21:54

     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self 
           action:@selector(aMethod1:)
    forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [view addSubview:button];   
    

提交回复
热议问题