iPhone Dev - Create UIButton Manually

I'm learning how to develop on the iPhone, I bought a book called Beginning iPhone 3 development Exploring the SDK. After I bit I decided to ditch Interface Builder. I still design all my views in IB, but I write It all in code and only use the nib file to get the controls' frames.

So now I need to make a UIButton, and the documentation is different from the other controls. I tried using initWithFrame:, and theres this other method buttonWithType: which I assume is autoreleased, but anyway I couldn't get a button to appear on the screen. Could someone please write a bit of code that locally creates a button with a title I can change that I can then just add to my views subview and release so I can see how it's done?

Answers


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.


Need Your Help

Liferay 6.1.1 CE GA2 domain's root redirects according session

session redirect liferay root

I've a strange feature in my environment. When I 1st arrive to my domain's root url it redirects me correctly /home. But if I click any sub pages and try to go to domain's root it redirects me back...

Alpha-Beta cutoff

search tree

I understand the basics of this search, however the beta cut-off part is confusing me, when beta <= value of alphabeta I can either return beta, break, or continue the loop.