iPhone Dev - Create UIButton Manually(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
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- UITextView 内容插图 2022-01-01
- URL编码Swift iOS 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01