UIButton button = new UIButton(); button.SetTitle("Press Me", UIControlState.Normal); button.Frame = new CoreGraphics.CGRect(0,0,100,50); button.AddTarget(this, new ObjCRuntime.Selector("ButtonClicked"), UIControlEvent.TouchUpInside);
void ButtonClicked() { Console.WriteLine("Button clicked"); } UIButton button = new UIButton(); button.SetTitle("Press Me", UIControlState.Normal); button.Frame = new CoreGraphics.CGRect(0,0,100,50); button.AddTarget(ButtonClicked, UIControlEvent.TouchUpInside);This example is similar to the previous one, but instead of specifying the target object as 'this', we are passing the ButtonClicked method directly. This can be useful when the method is static or located in another object. Both examples use the UIControlEvent.TouchUpInside event, which is triggered when the button is released inside its bounds. The UIButton class is part of the UIKit framework, which is included in the iOS development libraries.