Objective-C:以编程方式从另一个类添加 UIButtons
我无法连接代码和 .xib 文件之间的点。
如果我想以编程方式将一系列 UIButtons
添加到我的视图控制器中,我将如何从单独的类中执行此操作?
例如,如果我有 MainViewController.m
,它在 Xcode 中设置为根视图控制器,如何从 SecondViewController 将
?这可能吗?UIButton
添加到该视图控制器.m
我本质上想将所有“用户界面”代码放在一个单独的类中。
谢谢
I'm having trouble connecting the dots between code and .xib files.
If I want to add a series of UIButtons
programmatically to my view controller, how would I go about doing this from a separate class?
For instance, if I have MainViewController.m
, which is set as the root view controller in Xcode, how can I add a UIButton
to that view controller from SecondViewController.m
? Is this even possible?
I would essentially like to place all of my "user interface" code in a separate class.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,请以编程方式创建一个
UIButton *myButton
,然后调用[mainViewController addSubview:myButton];
。这可能意味着您需要在SecondViewController
类中存储MainViewController *
属性。UIButton< 的重要方法和属性/code>
实例(本质上,只需查看文档,但这里有一组最小的内容可以帮助您入门):
+[UIButton buttonWithType:buttonType]
- 确保您是否正在执行任何远程自定义操作以在此处使用UIButtonTypeCustom
(它不会为您提供任何默认背景图像,否则必须nil
out)setFrame:
- 相对于其容器定位按钮并设置大小,出于可用性原因,设置宽度 和
height
应至少为44
像素(如前所述此处)。setTitle:forState:
-UIControlStateNormal
也将充当其他状态的默认属性,因此您可能只需要在此处设置文本setBackgroundImage:forState:< /代码> - 主要使用
UIControlStateNormal
和UIControlStateHighlighted
/UIControlStateSelected
,如果您希望将其显示为灰色或无法访问,则使用UIControlStateDisabled
点。setImage:forState:
- 用于按钮文本旁边的图标(例如向下表示“保存”或向上表示“加载”的箭头等)setEnabled:
、setHidden:
,setSelected:
- 不同按钮状态之间的转换。setHighlighted:
在您点击按钮时自动发生。addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside
- TouchUpInside 几乎总是您想要的简单按钮按下操作,我正在使用名为buttonClicked 的方法:
在这里处理我的按钮按下操作。哦,如果您使用
[[UIButton alloc] initWith...]
一旦将其添加到mainViewController
,请不要忘记[myButton release]
代码>:)To do this, create a
UIButton *myButton
programmatically and then call[mainViewController addSubview:myButton];
. This may mean you need to store aMainViewController *
property in yourSecondViewController
class.Important methods and properties for a
UIButton
instance (essentially, just take a look at the documentation, but here's a minimal set of stuff to get you started):+[UIButton buttonWithType:buttonType]
- Make sure if you're doing anything remotely custom to useUIButtonTypeCustom
here (it doesn't give you any default background images or otherwise to have tonil
out)setFrame:
- Position the button relative to its container and set the size, for usability reasons thewidth
andheight
should be at least44
pixels (as mentioned here).setTitle:forState:
-UIControlStateNormal
will act as the default properties for other states too, so you may only need to set the text heresetBackgroundImage:forState:
- useUIControlStateNormal
andUIControlStateHighlighted
/UIControlStateSelected
primarily,UIControlStateDisabled
if you wish to show it grayed out or inaccessible at any point.setImage:forState:
- Use for an icon next to the button text (like an arrow pointing down for Save or up for Load, etc)setEnabled:
,setHidden:
,setSelected:
- Transition between different button states.setHighlighted:
happens automatically when you tap the button.addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside
- TouchUpInside is almost always what you want for a simple button press, I'm using a method namedbuttonClicked:
here to handle my button press.Oh, and if you use
[[UIButton alloc] initWith...]
don't forget to[myButton release]
once it's added to themainViewController
:)使用它
在您的实现中
在您的 MainViewcontroller.m 中
use this
In your implementation
In your MainViewcontroller.m