有什么好的、可维护的方法来命名 IBActions 调用的方法?

发布于 2024-10-03 06:30:52 字数 1431 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-10-10 06:30:52

这是一个大问题!

纯粹FWIW......我的投票是“theSaveButton”“theButtonAtTheTopRight”“userClickedTheLaunchButton”“doubleClickedOnTheRedBox”等等。

一般来说,我们都这样命名所有这些例程。然而..通常我只是让他们直接进入另一个例程“launchTheRocket”“saveAFile”等等。

事实证明这有用吗?这是因为您经常想自己发射火箭......在这种情况下调用 launchTheRocket 例程,而不是用户按下按钮然后发射火箭。如果你想自己发射火箭,然后调用 userClickedTheLaunchButton,感觉不太对,而且代码看起来更混乱。 (您是否正在尝试专门模拟屏幕上的按下,或者?)当它们分开时,调试等会更容易,因此您知道谁调用了什么。

事实证明,它在收集统计数据等方面稍微有用。用户请求火箭发射 198 次,总共我们发射了 273 次火箭。

此外 - 这可能是关键 - 比如说,从代码的另一部分,您正在使用 launch-therocket 消息来发射火箭。它更清楚地表明您实际上正在执行此操作,而不是与按钮有关。相反,userClickedTheLaunchButton 概念可能会随着时间的推移而改变,它通常可能会发射火箭,但有时它可能只是显示一条消息,或者谁知道什么。

事实上,单击按钮还可能触发辅助内容(可能是动画等),这是执行此操作的最佳位置,在“clickedTheButton”内部,然后调用勇敢的函数“launchTheRocket”。

因此,我实际上提倡第三种更加复杂的解决方案,即具有单独的“userDidThis”函数,然后具有单独的“startANewGame”函数。即使这意味着通常前者几乎什么都不做,只是调用后者!

顺便说一句,另一个命名选项是将两者结合起来......“topButtonLaunchesRockets”“glowingCubeConnectsSocialWeb”等。

最后!不要忘记,您通常可能会将它们设置为一个动作,这会在风格上改变一切。

[theYellowButton addTarget:.. action:@selector(launchRockets) ..];
[theGreenButton addTarget:.. action:@selector(cleanUpSequence) ..];
[thatAnimatingButtonSallyBuiltForUs addTarget:.. action:@selector(resetAll) ..];
[redGlowingArea addTarget:.. action:@selector(tryGetRatingOnAppStore) ..];

也许这是最好的方式,从记录角度来看!

It's a huge problem!

Purely FWIW ... my vote is for "theSaveButton" "theButtonAtTheTopRight" "userClickedTheLaunchButton" "doubleClickedOnTheRedBox" and so on.

Generally we name all those routines that way. However .. often I just have them go straight to another routine "launchTheRocket" "saveAFile" and so on.

Has this proved useful? It has because often you want to launch the rocket yourself ... in that case call the launchTheRocket routine, versus the user pressing the button that then launches the rocket. If you want to launch the rocket yourself, and you call userClickedTheLaunchButton, it does not feel right and looks more confusing in the code. (Are you trying to specifically simulate a press on the screen, or?) Debugging and so on is much easier when they are separate, so you know who called what.

It has proved slightly useful for example in gathering statistics. The user has requested a rocket launch 198 times, and overall we've launched the rocket 273 times.

Furthermore -- this may be the clincher -- say from another part of your code you are launching the rocket, using the launch-the-rocket message. It makes it much clearer that you are actually doing that rather than something to do with the button. Conversely the userClickedTheLaunchButton concept could change over time, it might normally launch the rocket but sometimes it might just bring up a message, or who knows what.

Indeed, clicking the button may also trigger ancillary stuff (perhaps an animation or the like) and that's the perfect place to do that, inside 'clickedTheButton', as well as then calling the gutsy function 'launchTheRocket'.

So I actually advocate the third even more ridiculously complicated solution of having separate "userDidThis" functions, and then having separate "startANewGame" functions. Even if that means normally the former does almost nothing, just calling the latter!

BTW another naming option would be combining the two... "topButtonLaunchesRockets" "glowingCubeConnectsSocialWeb" etc.

Finally! Don't forget you might typically set them up as an action, which changes everything stylistically.

[theYellowButton addTarget:.. action:@selector(launchRockets) ..];
[theGreenButton addTarget:.. action:@selector(cleanUpSequence) ..];
[thatAnimatingButtonSallyBuiltForUs addTarget:.. action:@selector(resetAll) ..];
[redGlowingArea addTarget:.. action:@selector(tryGetRatingOnAppStore) ..];

perhaps that's the best way, documentarily wise!

慵挽 2024-10-10 06:30:52

我还会使用类似 xButtonPressed: 或 handleXTap: 的方法,然后从处理程序中调用另一个方法。

- (IBAction)handleDoneTap:(id)sender {
  [self closeView];
}

- (void)closeView {
  if ([self validate]) {
    // save and close
  }
  else {
    // display error information
  }
}

I would also go with something along the lines of xButtonPressed: or handleXTap: and then call another method from within the handler.

- (IBAction)handleDoneTap:(id)sender {
  [self closeView];
}

- (void)closeView {
  if ([self validate]) {
    // save and close
  }
  else {
    // display error information
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文