iPhone Dev - 手动创建UIButton [英] iPhone Dev - Create UIButton Manually

查看:94
本文介绍了iPhone Dev - 手动创建UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在iPhone上开发,我买了一本名为Beginning iPhone 3开发探索SDK的书。在我咬了一口之后,我决定抛弃Interface Builder。我仍然在IB中设计我的所有视图,但是我在代码中编写了所有视图,并且只使用nib文件来获取控件的帧。

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.

所以现在我需要制作a UIButton ,文档与其他控件不同。我尝试使用 initWithFrame:,而另外这个方法 buttonWithType:我假设是自动释放的,但无论如何我无法' t得到一个按钮出现在屏幕上。有人可以写一些代码,在本地创建一个标题我可以更改的按钮,然后我可以添加到我的视图子视图和发布所以我可以看到它是如何完成的?

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?

推荐答案

我会尝试这样的事情:

    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];

免责声明:只需在此输入即可。我目前无法访问我的Mac,所以我无法测试它。

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。任何特殊原因不使用Interface Builder?好奇。

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

这篇关于iPhone Dev - 手动创建UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆