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

后端 未结 10 1247
野的像风
野的像风 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

    CGRect buttonFrame = CGRectMake( 10, 80, 100, 30 );
            UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
            [button setTitle: @"My Button" forState: UIControlStateNormal];
            [button addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
    [view addSubview:button];
    

提交回复
热议问题