Uibutton - Alloc Initwithframe: VS. Buttonwithtype:

In iOS, can a UIButton be added by alloc and initWithFrame?

Yes of course it can. I got into a little bit of a discussion as to that readonly buttonType property a while back. The general concensus is that there is neither a safe way, nor a good reason, for anyone to switch the type of a button. And besides, +buttonWithType returns an instance of UIButton just as well as +Alloc and -init do, so it isn't a problem.

How to init UIButton's type after using alloc?

Why even have to alloc and init if +buttonWithType handles that for you? Just reverse your thinking like this:

UIButton * button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[button setFrame:CGRectMake(20, 300, 100, 50)];

How do I create a basic UIButton programmatically?

Here's one:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

iPhone Dev - Create UIButton Manually

I'd try something like this:

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
[myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[superView addSubview:myButton];

Disclaimer: Just typing this in here. I don't have access to my Mac at the moment so I can't test it.

P.S. Any particular reason not to use Interface Builder? Just curious.

Why doesn't UIButton title display?

I cannot tell you why it does not work, but I do have a solution:

UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;        
b. frame = CGRectMake(0, 0, 100, 100);

[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self addSubview:b];

Seperate creating the frame from the allocation and init of the button.

how to set UIButton type in UIButton Subclass

You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:

Note that this way the buttonType isn't explicitly set to anything,
which probably means that it's UIButtonTypeCustom. The Docs don't
seem to actually specify that, but since that's the 0 value in the
enum, that's likely what happens (and that seems to be the observable
behavior as well)

create uibutton subclass

You don’t really want to subclass UIButton. It’s a class cluster, so individual instances will be something like UIRoundRectButton or some other private Apple class. What are you trying to do that requires a subclass?



Related Topics



Leave a reply



Submit