简体   繁体   English

在iOS上以编程方式添加UIBarButtonItem操作

[英]Add UIBarButtonItem action programmatically on iOS

I have several UIBarButtonItems, and I want to set the action when you press it programmatically. 我有几个UIBarButtonItems,当您以编程方式按下它时,我想设置该动作。 How can I do this? 我怎样才能做到这一点?

EDIT: if it would be possible non-programmatically, please also reply :) 编辑:如果有可能以非编程方式,也请回复:)

The action is going to Google.com, and the button is the search button in this project: (download): click here 该操作将转到Google.com,该按钮是该项目中的搜索按钮:(下载): 单击此处

First create an outlet to the items in IB. 首先为IB中的项目创建一个出口。 Then you can modify the methods called by using [myItem setTarget:] and [myItem setAction:]. 然后,您可以修改使用[myItem setTarget:]和[myItem setAction:]调用的方法。

See target action 查看目标动作

ETA: Is there any particular reason you want to do it programmatically? ETA:您是否有特定原因要通过编程方式进行?

ETAA: To do it non-programmatically, make a function with return type IBAction, and argument (id)sender, like so: ETAA:要以非编程方式执行此操作,请使用返回类型IBAction和参数(id)sender来创建函数,如下所示:

- (IBAction)myMethod:(id)sender;

With whatever implementation you like. 通过任何您喜欢的实现。 Then, in IB, drag an object object from the Library to the MainWindow.xib window, and in the identity window (cmd-6), give it the class that has your function. 然后,在IB中,将对象对象从库中拖到MainWindow.xib窗口中,并在标识窗口(cmd-6)中,为其提供具有您的功能的类。 Then, in the connections window of that object (cmd-5), you can drag from your function name to whichever interface object you want. 然后,在该对象的连接窗口(cmd-5)中,可以从函数名称拖动到所需的任何接口对象。

I strongly suggest you read mac dev on IB . 我强烈建议您阅读IB上的mac dev The connections and bindings section will discuss this particular issue. 连接和绑定部分将讨论此特定问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM