在运行时设置按钮选择器(点击事件)? (可可:Mac项目) [英] Set Button selector (on click event) at runtime ? (Cocoa : Mac Project)

查看:215
本文介绍了在运行时设置按钮选择器(点击事件)? (可可:Mac项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法如何在运行时(点击事件)为mac(不是IOS)设置按钮选择器



我有一个视图控制器的按钮声明为outlet:

  #import< Cocoa / Cocoa.h> 

@interface MyView:NSView
@property(assign)IBOutlet NSButton * MyBtn;

我可以使用这样的东西:

  MyView * v; 



[v MyBtn]

我没有找到addTarget 。
唯一的选择是setTarget(id)



我使用Xcode 4.4.1 for osx mountain lion



任何建议将不胜感激。感谢

解决方案

您将看到 NSControl 类别参考 NSButton 继承)两个单独的方法 setTarget: setAction: p>

您可以在代码中执行类似的操作:

  [MyBtn setTarget :自]; 
[MyBtn setAction:@selector(doStuff)];

请参阅此回答


Any idea how to set button selector at runtime (on click event) for mac (Not IOS)

I have a view controller with a button declared as outlet :

#import <Cocoa/Cocoa.h>

@interface MyView : NSView
@property (assign) IBOutlet NSButton *MyBtn;

i can access that buton with something like this :

MyView * v;
.
.
.
[v MyBtn]

i didn't find "addTarget" choice like on cocoa touch. The only choice is "setTarget(id)"

I'm using Xcode 4.4.1 for osx mountain lion

any suggestion would be appreciated. thanks

解决方案

You shall look at the NSControl class reference (from which NSButton inherits) for two separate methods setTarget: and setAction:.

You can do something like this in your code:

[MyBtn setTarget:self];
[MyBtn setAction:@selector(doStuff)];

take a look also at this answer.

这篇关于在运行时设置按钮选择器(点击事件)? (可可:Mac项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆