且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS 在两个方法之间传递一个字符串

更新时间:2023-02-22 12:39:30

点击没有用,只能再次比较.为什么不将 @selector(handleCorrectAnswer:) 操作绑定到正确的按钮,将 @selector(handleIncorrectAnswer:) 操作绑定到其他按钮?在您的代码中,您知道哪个是正确的,哪些不是.您应该需要在另一个函数中再次弄清楚这一点.

No use clicking to only compare again. Why not tie @selector(handleCorrectAnswer:) action to the correct button and @selector(handleIncorrectAnswer:) action to the others? At that point in your code, you know which is the correct one and which ones aren't. You should need to figure that out again in another function.

另外,我假设您正在做一个微不足道的学习练习.如果这是一个真正的应用程序,您可能希望将问题和答案具体化为数据(文件、数据库等),并且处理它的代码将是通用的.您上面的代码是相当硬编码的,但如果它只是一个学习实验,那就没问题了.

Also, I assume you're doing a trivial learning exercise. If this was a real app, you would want to externalize the questions and answer as data (file, db etc...) and the code to handle it would be generic. Your code above is pretty hard coded but that's fine if it's just a learning experiment.

此外,您还询问了标题 (.h) 中的 @interface.这就是您为类定义接口(方法和属性定义)的地方.在我的建议中,这意味着您会添加:

Also, you asked about the @interface in header (.h). That's where you define the interface (method and property definitions) for the class. In my suggestion, that means you would add:

@interface MyClass

- (IBAction)handleCorrectAnswer:(id)sender;
- (IBAction)handleIncorrectAnswer:(id)sender;

然后你会在你的 .m 中实现

Then you would implement in your .m