Objective-C:以编程方式从另一个类添加 UIButtons

发布于 2024-12-08 18:04:10 字数 298 浏览 0 评论 0原文

我无法连接代码和 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年慕年 2024-12-15 18:04:10

为此,请以编程方式创建一个 UIButton *myButton,然后调用 [mainViewController addSubview:myButton];。这可能意味着您需要在 SecondViewController 类中存储 MainViewController * 属性。

UIButton< 的重要方法和属性/code>实例(本质上,只需查看文档,但这里有一组最小的内容可以帮助您入门):

  • +[UIButton buttonWithType:buttonType] - 确保您是否正在执行任何远程自定义操作以在此处使用 UIButtonTypeCustom (它不会为您提供任何默认背景图像,否则必须 nil out)

  • setFrame: - 相对于其容器定位按钮并设置大小,出于可用性原因,设置宽度 和 height 应至少为 44 像素(如前所述此处)。

  • setTitle:forState: - UIControlStateNormal 也将充当其他状态的默认属性,因此您可能只需要在此处设置文本

  • setBackgroundImage:forState:< /代码> - 主要使用 UIControlStateNormalUIControlStateHighlighted/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 a MainViewController * property in your SecondViewController 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 use UIButtonTypeCustom here (it doesn't give you any default background images or otherwise to have to nil out)

  • setFrame: - Position the button relative to its container and set the size, for usability reasons the width and height should be at least 44 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 here

  • setBackgroundImage:forState: - use UIControlStateNormal and UIControlStateHighlighted/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 named buttonClicked: 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 the mainViewController :)

故笙诉离歌 2024-12-15 18:04:10

使用它

 #import "MainViewController.h"

@interface SecondViewController
{
  MainViewController *mainView;
}

@property(nonatomic, retain)  MainViewController *mainView;

-(void)addButtons;

在您的实现中

@synthesize mainView;

-(void)addButtons
{
  UIButton *add = [UIButton alloc] init];
//do necessary stuff on button here
[self.mainView addSubview:add];
[add release];
}

在您的 MainViewcontroller.m 中

#import "SecondViewController.h"

-(void)viewDidLoad
{
[self superViewDidLoad];
SecondViewController *second = [SecondViewController alloc] init];
second.mainView = self;
[second addButton];
[second release];
}

use this

 #import "MainViewController.h"

@interface SecondViewController
{
  MainViewController *mainView;
}

@property(nonatomic, retain)  MainViewController *mainView;

-(void)addButtons;

In your implementation

@synthesize mainView;

-(void)addButtons
{
  UIButton *add = [UIButton alloc] init];
//do necessary stuff on button here
[self.mainView addSubview:add];
[add release];
}

In your MainViewcontroller.m

#import "SecondViewController.h"

-(void)viewDidLoad
{
[self superViewDidLoad];
SecondViewController *second = [SecondViewController alloc] init];
second.mainView = self;
[second addButton];
[second release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文