我如何知道点击了哪个按钮?目标C

发布于 2024-08-07 05:54:56 字数 204 浏览 1 评论 0原文

我正在作业中做 Presence1,这要求我构建一个多屏幕应用程序。 我有两个 ViewController,vc1 和 vc2。在vc1中,我有两个按钮。我对它们使用相同的方法,并且它们的标题相同。

我的问题是,当我更改为 vc2 时,如何知道在 vc1 中单击了哪个按钮?

有一个主题告诉我,当我单击按钮时,我应该获取鼠标的位置(x,y),但我认为这不太好。

I am doing Presence1 in Assignment which require me builds a multiple screens application.
I have two ViewController, vc1 and vc2. In vc1, i have two buttons. I use a same method for them and the title of them are same.

My question is How can i know which button is clicked in vc1 when i change to vc2?

There are a topic show me that I should get positions (x,y) of mouse when i click on button, but i think it is not quite good.

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

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

发布评论

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

评论(2

机场等船 2024-08-14 05:54:56

上面的答案会起作用。如果您不想保留按钮的插座,您可以在界面生成器中为它们分配标签。例如,您为按钮 1 分配标签值 1,为按钮 2 分配标签值 2。然后在代码中

-(void)onButtonClick:(id)sender {
    if(sender.tag == 1) {
        //respond to button 1
    } else if(sender.tag == 2) {
        //respond to button 2
    }
}

The answer above would work. If you don't want to keep outlets for the buttons you can assign them tags in interface builder. As an example, you assign button 1 a tag value of 1 and button 2 a tag value of 2. Then in the code

-(void)onButtonClick:(id)sender {
    if(sender.tag == 1) {
        //respond to button 1
    } else if(sender.tag == 2) {
        //respond to button 2
    }
}
假装爱人 2024-08-14 05:54:56

如果您有两个 NSButton 属性,例如:

@interface ViewControllerOne : NSViewController
{
    NSButton *goButton;
    NSButton *stopButton;
}

@property(nonatomic, retain) NSButton *goButton;
@property(nonatomic, retain) NSButton *stopButton;

-(void)onButtonClick:(id)sender;

@end

那么您可以将发送者与按钮指针进行比较:

-(void)onButtonClick:(id)sender
{
    if (sender == goButton) {
    }

    else if (sender == stopButton) {
    }
}

这就是您想要的吗?

If you have two NSButton properties such as:

@interface ViewControllerOne : NSViewController
{
    NSButton *goButton;
    NSButton *stopButton;
}

@property(nonatomic, retain) NSButton *goButton;
@property(nonatomic, retain) NSButton *stopButton;

-(void)onButtonClick:(id)sender;

@end

Then you can compare the sender with the button pointer:

-(void)onButtonClick:(id)sender
{
    if (sender == goButton) {
    }

    else if (sender == stopButton) {
    }
}

Is that what you're after?

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