使用大量 IBOutlet 是一个好习惯吗?
我仍然是 iOS 的新手,所以最近我做了很多教程。
假设我正在制作一个应用程序,例如计算器,有 24 个按钮。我见过示例代码,其中按钮的标签用于确定它是什么按钮,但这看起来确实很混乱,尤其是在尝试翻译应用程序时。
那么,是不是硬着头皮为每个按钮配备一个 IBOutlet 会更好呢?为什么或为什么不?
如果不是,那么在保持 MVC 范式的同时,最优雅的方式是什么?
好吧,我只是回顾一下我的代码,现在我感觉自己比以前更像菜鸟了...我真的在谈论 IBActions,而不是 IBOutlet...我应该为不同的按钮提供一大堆 IBActions 吗?这就是 viewController.h 文件中现在的样子:
- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
- (IBAction)dotPressed:(UIButton *)sender;
- (IBAction)button_mClear_Pressed:(UIButton *) sender;
- (IBAction)button_mPlus_Pressed:(UIButton *) sender;
- (IBAction)button_mMinus_Pressed:(UIButton *) sender;
- (IBAction)button_mRecall_Pressed:(UIButton *) sender;
- (IBAction)button_AC_Pressed:(UIButton *) sender;
- (IBAction)button_PlusMinus_Pressed:(UIButton *) sender;
为什么我觉得它重复且不优雅?
I'm still quite the beginner at iOS and so I've been doing lots of tutorials, lately.
Let's say I was making an app such as a calculator, with let's say 24 buttons. I've seen example code where the button's label gets used to figure out what button it is, but that seems really kludgey, especially when trying to translate the app.
So is it better to just bite the bullet and have one IBOutlet for each and every button instead? why or why not?
If not, what would be the most elegant way to go about doing this, while staying in the MVC paradigm?
Ok I just was looking back at my code and now i feel more like a noob than before... I really was talking about IBActions, not so much IBOutlets... Should I have a whole bunch of IBActions for the different buttons? here's what it looks like right now in the viewController.h file:
- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
- (IBAction)dotPressed:(UIButton *)sender;
- (IBAction)button_mClear_Pressed:(UIButton *) sender;
- (IBAction)button_mPlus_Pressed:(UIButton *) sender;
- (IBAction)button_mMinus_Pressed:(UIButton *) sender;
- (IBAction)button_mRecall_Pressed:(UIButton *) sender;
- (IBAction)button_AC_Pressed:(UIButton *) sender;
- (IBAction)button_PlusMinus_Pressed:(UIButton *) sender;
why does that just feel repetitive and inelegant to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,相似的按钮都会触发相同的操作,其想法是相似的按钮操作之间应该有一些通用的代码。然后,您可以使用
tag
属性来识别单击了哪个按钮。例如,数字按钮触发特定操作,操作按钮触发另一个操作,等等。您可以在 Interface Builder 中的任何控件上设置
tag
属性。Typically, you'd have similar buttons all trigger the same action, the idea being that similar button actions should have some common code between them. You then use the
tag
property to identify which button was clicked. E.g., number buttons trigger a specific action, operator buttons trigger another action, and so on.You can set the
tag
property on any control in Interface Builder.如果您使用 IBOutlet 并在 Interface Builder/Xcode 4 中将它们连接起来,这更多是一个品味问题,而不是编程决策。而且这样做与否并不一定会影响mvc范式。
这是您的选择,如果您在视图控制器中保留 24 个 IBOutlet 并从笔尖加载按钮,因为将它们排列在您的界面中可能更容易,或者拥有一个充满按钮的数组,然后以编程方式将它们添加到您的视图中,用正确的行动来设置它们。
您还可以在不同的笔尖中为不同的视图控制器设置按钮 - 比如数字键盘、简单命令以及高级命令和功能。每个视图控制器都会有一个特定协议的委托,所有这些协议都将由“BrainController”来实现。对于一个简单的计算器来说,这个设置可能有点过大,但允许您使用笔尖,而不会让视图控制器被 IBOutlet 过度拥挤。 。您可以在其他项目中重复使用它的一部分,即带有远程控制界面的应用程序中的数字键盘。
If you use IBOutlets and wire them up in Interface Builder/Xcode 4 is more a matter of taste, than a programming decision. And doing so or not does not necessarily affect the mvc paradigm.
It is your choice, if you keep 24 IBOutlets in your viewcontroller and load the buttons from a nib, as it is maybe easier to arrage them in your interface, or to have an array full of buttons, and add them to your view programmatically and set them up with the right actions.
You can also have the buttons in different nibs for different viewcontroller — lets say for the number pad, the simple commands and the higher commands and functions. each of the viewcontrollers would have a delegate of a certain protocol, which all would be implemented by on 'BrainController'.This setup might be a bit overkill for a simple calculator, but would allow you to use nibs, without a viewcontoller overcrowded with IBOutlets. And you could re-use oarts of it in other project, i.e. the numberpad in an app with a remote control interface.
如果您使用 XIB 和很多对象,那么可以。如果您打算让对象执行一些特殊操作,例如在代码中稍后的某些方法调用期间禁用按钮,那么可以,连接 IBOutlet。如果您仅将按钮连接到 IBActions,那么不需要,只需将任何按钮(不带 IBOutlet)连接到您的 IBAction,这将节省您连接一堆对象的时间。
If you use XIB's and a lot of objects, then yes. If you plan on making the object do something special like disable the button during some method call later in code, then YES, hook up an IBOutlet. If you are only connecting the buttons to IBActions, then NO, just connect any button (without IBOutlet) to your IBAction, this will save you on connecting a bunch of objects.