iPhone Dev - 手动创建 UIButton

iPhone Dev - Create UIButton Manually(iPhone Dev - 手动创建 UIButton)

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

问题描述

我正在学习如何在 iPhone 上进行开发,我买了一本名为《Beginning iPhone 3 development Exploring the 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.

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

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.

附:有什么特别的理由不使用 Interface Builder?只是好奇.

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

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

本文标题为:iPhone Dev - 手动创建 UIButton

基础教程推荐