views:

46

answers:

2

Hello, i'm tring to extend UIButton class to add my id property.

How i can do this?

Problem is to create a button you use

var button = UIButton.FromType(UIButtonType.RoundRect);

so, if i've

public class MyButton : UIButton

how i can create my button?

thanks.

A: 

Here is how you would create your button:

MyButton button = new MyButton (new RectangleF(0, 0, 250, 37));
button.SetTitle("My Button",UIControlState.Normal);

To Create a button of a type you could try:

MyButton button = new MyButton (UIButtonType.RoundRect);

and see if it returns a button of the correct type which inherits from your MyButton class - but I doubt it. You may have to add the image and size it yourself:

UIImage image = UIImage.FromFile("action.png");
MyButton button = new MyButton (new RectangleF(0, 0, 250, 37));
button.SetBackgroundImage(image,UIControlState.Normal);

Not sure if there's another way but looking at the docs the UIButton.ButtonType is read only and there's no SetType method so you can only set a UIButton class ButtonType property by instantiating it and passing the type into the constructor.

w://

cvista
Please explict MyButton class.Thanks
Premier
With your first code suggest, no buttons on screen.
Premier
Ok, i've added a background and now i see buttons.thanks.
Premier
A: 
class MyButton : UIButton
{
public MyButton(RectangleF rect) : base(rect) {}

static MyButton FromType(UIButtonType buttonType)
{
    var b = new MyButton (new RectangleF(0, 0, 200, 40));
    b.SetTitle("My Button",UIControlState.Normal);

    //additional customization here

    return b;
    }
}
ifwdev
Basically the same as cvista but using a static factory method to make it cleaner
ifwdev
i used your code, but there is a compilation error: The type MyButton does not contain a costructor that takes 1 arguments.Some ideas?
Premier
See constructor in edit. Sorry about that.
ifwdev