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

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

    UIButton *btnNotification=[UIButton buttonWithType:UIButtonTypeCustom];
    btnNotification.frame=CGRectMake(130,80,120,40);
    btnNotification.backgroundColor=[UIColor greenColor];
    [btnNotification setTitle:@"create Notification" forState:UIControlStateNormal];
    [btnNotification addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnNotification];
    
    
    }
    
    -(void)btnClicked
    {
        // Here You can write functionality 
    
    }
    

提交回复
热议问题