xcode 如何为 UIBarButtonItem 设置操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25118411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set an action for an UIBarButtonItem?
提问by basti12354
I try to set an action for an UIBarButtonItem like I did before with a normal button:
我尝试像以前使用普通按钮一样为 UIBarButtonItem 设置操作:
- Select the UIBarButtonItem in storyboard.
- Right-Click and pull to my .h-file
Change to action and choose UIBarButtonItem and give the action a name.
- (IBAction)haupt:(UIBarButtonItem *)sender;
Write some code in the function at my .m-file.
- (IBAction)haupt:(UIBarButtonItem *)sender { NSLog(@"button"); }
- 在情节提要中选择 UIBarButtonItem。
- 右键单击并拉到我的 .h 文件
更改为操作并选择 UIBarButtonItem 并为操作命名。
- (IBAction)haupt:(UIBarButtonItem *)sender;
在我的 .m 文件中的函数中编写一些代码。
- (IBAction)haupt:(UIBarButtonItem *)sender { NSLog(@"button"); }
Now I try this in the simulator, but nothing happens (No output on the console). What I am doing wrong?
现在我在模拟器中尝试这个,但没有任何反应(控制台上没有输出)。我做错了什么?
采纳答案by codeIgnitor
Select the UIBarButton , Create an IBAction by CNRL+Dragging at the implementation section in .m file then try to print with NSLog use breakpoint to know if the control is reaching the IBAction you created.
选择 UIBarButton ,通过 CNRL+Dragging 在 .m 文件的实现部分创建一个 IBAction,然后尝试使用 NSLog 打印,使用断点来了解控件是否到达您创建的 IBAction。
回答by Ondrej Kvasnovsky
Here is how you do it with Swift 3:
以下是使用 Swift 3 的方法:
let check: UIBarButtonItem = UIBarButtonItem()
check.title = "Check"
check.width = CGFloat(100)
check.tintColor = UIColor.green
check.action = #selector(ViewController.checkTapped)
check.target = self
func checkTapped (sender:UIBarButtonItem) {
print("check pressed")
}
回答by Himanshu padia
myBarButtonItem.target = self;
myBarButtonItem.action = @selector( myMethod: );
Remember action method must have the following signature:
请记住操作方法必须具有以下签名:
- ( IBAction )myMethod: ( id )sender;
or refer this link : Add a custom selector to a UIBarButtonItem
或参考此链接:将自定义选择器添加到 UIBarButtonItem
回答by dieter
You need to set an Action:
您需要设置一个操作:
[self.myButton setAction:@selector(haupt:)];
Or if you are using Storyboard. Right click und pull to you .m file (not .h)
或者,如果您正在使用故事板。右键单击并拉到您的 .m 文件(不是 .h)
回答by Alessandro Ornano
Swift 4:
斯威夫特 4:
If you have already create it you can use the customView
property:
如果您已经创建了它,您可以使用该customView
属性:
barButton.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(barButtonTap)))
回答by Kalpit Gajera
You can use belowed code it is working
您可以使用下面的代码,它正在工作
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]init];
barButton.title=@"Back";
[barButton setTarget:self];
[barButton setAction:@selector(uiBarBurronAction:)];
Button Action
按钮操作
-(IBAction)uiBarBurronAction:(id)sender
{
NSLog(@"barbutton Pressed")
}
Otherwise you can set selector RightClick on uibarbuttonitem and drag selector and give action to viewcontroller
否则,您可以在 uibarbuttonitem 上设置选择器 RightClick 并拖动选择器并为视图控制器提供操作
回答by Bernhard Grabowski
You ctrl-drag the button to you header file and create an IBOutlet.
您按住 ctrl 并拖动按钮到您的头文件并创建一个 IBOutlet。
Then you wire the button with the IBAction, all good.
然后你用 IBAction 连接按钮,一切都很好。