从另一个按钮更改一个按钮的标题 IBAction

发布于 2025-01-02 23:43:54 字数 158 浏览 4 评论 0原文

以编程方式创建 UIbuttons 时,是否可以从另一个 UIbuttons IBAction 方法访问 UIbutton?我能够使用界面生成器通过为每个按钮创建一个 IBOutlet 并以这种方式引用它们来完成此操作,但我需要以编程方式创建按钮,并且似乎无法弄清楚这是否可能?

谢谢!

Is there anyway to access a UIbutton from another UIbuttons IBAction method when creating the UIbuttons programmatically? I was able to do this using interface builder by creating an IBOutlet for each of them and referencing them that way, but I need to create the buttons programmatically and can't seem to figure out if this is possible?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

生生不灭 2025-01-09 23:43:54

创建按钮时,将它们分配给控制器中的 ivars。然后您的 IBAction 方法可以根据需要访问这些 ivars 中的任何一个。

例如,在您的视图控制器标头中,您可能有:

@interface MyViewController : UIViewController

@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;

@end

在您的实现中,您将创建如下所示的按钮:

UIButton *b = [[UIButton alloc] init...];
...more setup...
self.button1 = b;
[b release];

...

然后在您的 IBAction 方法中,您可能会执行如下操作:

-(IBAction)buttonPress:(UIButton *)b
{
    if (b == self.button1)
        // do something to button2
    else
        // do something to button1
}

有意义吗? (这都是从内存中编码的,未经测试,fwiw。)

When you create the buttons, assign them to ivars in your controller. Then your IBAction methods can access any of these ivars as needed.

For example, in your view controller header you might have:

@interface MyViewController : UIViewController

@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;

@end

In your implementation you would create your buttons something like this:

UIButton *b = [[UIButton alloc] init...];
...more setup...
self.button1 = b;
[b release];

...

Then in your IBAction method you might do something like this:

-(IBAction)buttonPress:(UIButton *)b
{
    if (b == self.button1)
        // do something to button2
    else
        // do something to button1
}

Make sense? (This is all coded from memory and not tested, fwiw.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文