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

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

    Try this:

    Swift 4

    myButton.addTarget(self,
                       action: #selector(myAction),
                       for: .touchUpInside)
    

    Objective-C

    [myButton addTarget:self 
                 action:@selector(myAction) 
       forControlEvents:UIControlEventTouchUpInside];
    

    You can find a rich source of information in Apple's Documentation. Have a look at the UIButton's documentation, it will reveal that UIButton is a descendant of UIControl, which implements the method to add targets.

    --

    You'll need to pay attention to whether add colon or not after myAction in action:@selector(myAction)

    Here's the reference.

    0 讨论(0)
  • 2020-11-29 21:48

    IOS 14 SDK: you can add action with closure callback:

    let button = UIButton(type: .system, primaryAction: UIAction(title: "Button Title", handler: { _ in
                print("Button tapped!")
            }))
    

    Getting a reference to the control sender

    let textField = UITextField()
    textField.addAction(UIAction(title: "", handler: { action in
        let textField = action.sender as! UITextField
        print("Text is \(textField.text)")
    }), for: .editingChanged)
    

    source

    0 讨论(0)
  • 2020-11-29 21:53
     CGRect buttonFrame = CGRectMake( x-pos, y-pos, width, height );   //
     CGRectMake(10,5,10,10) 
    
     UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
    
     button setTitle: @"My Button" forState: UIControlStateNormal];
    
     [button addTarget:self action:@selector(btnClicked:) 
     forControlEvents:UIControlEventTouchUpInside];
    
     [button setTitleColor: [UIColor BlueVolor] forState:
     UIControlStateNormal];
    
     [view addSubview:button];
    
    
    
    
     -(void)btnClicked {
        // your code }
    
    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 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];   
    
    0 讨论(0)
  • 2020-11-29 21:56

    Swift answer:

    myButton.addTarget(self, action: "click:", for: .touchUpInside)
    
    func click(sender: UIButton) {
        print("click")
    }
    

    Documentation: https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

    0 讨论(0)
提交回复
热议问题