在 UIViewController 中,我怎样才能有一个方法自动处理很多按钮
我有一个用于 iPad 应用程序的 UIViewController,用户可以通过触摸 80 个按钮之一来选择 80 个谜题之一。有没有办法以编程方式或其他巧妙的方式来完成此操作,而不是手动将 80 UIButton
连接到 IBAction
方法?
当然,另一种选择是构建自定义视图并自己完成所有工作,但我想知道上述操作是否可以变得更容易。
I have a UIViewController
for an iPad app where the user can pick one of 80 puzzles by touching one of 80 buttons. Rather than manually connect 80 UIButton
to an IBAction
method, is there any way to do this programmatically or in some other clever way?
Another option of course to build a custom view and do all the work myself but I would like to know if the above can be made easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的意思是 80 个 UIButtons 对应 80 个操作,还是 80 个按钮对应 1 个操作?
您可以使用以下命令以编程方式添加所有按钮:
.h
.m
以及链接到一个名为
“etc”的操作的所有其他按钮
Do you mean 80 UIButtons to 80 actions or 80 buttons to 1 action?
You could add all the buttons programmatically using this:
.h
.m
And all other buttons linked to one action called:
etc
像这样的东西可能会起作用:
something like this might work:
您可以列出视图中的对象(子视图)以查找所有按钮。当然,您需要以某种方式清除其他对象。
您可以为按钮执行 addTarget 来设置其操作方法。
每个操作方法都会接收一个指向 UI 对象的指针。您只需将指针与对象列表进行比较即可确定它是哪一个。
You can list the objects (subviews) in a view to find all your buttons. Of course, you'll need to weed out other objects somehow.
You can do addTarget for the buttons to set their action methods.
Every action method receives a pointer to the UI object. You just need to compare the pointer to your list of objects to figure out which one it is.
Interface Builder 将允许您将 80 个对象连接到 1 个方法,我认为您可以通过多个选择一次性完成所有操作(我会仔细检查)。然后,在该方法中,您可以使用 (id)sender值来确定它是哪个按钮;例如,
如果你的标题是连续的,你可以循环它们:
Interface Builder will let you connect 80 objects to 1 method, and I think you might be able to do it all at once with multiple select (I will double-check.) Then, within that method, you can use the (id)sender value to determine which button it is; for example, by saying
If your titles are consecutive, you could loop through them: