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

发布于 2024-12-19 14:27:38 字数 1684 浏览 0 评论 0原文

我有 4 个选项分配给 4 个按钮。其中一个选项是正确的,它被分配给字符串“ CorrectAnswerString”

4 个按钮调用“action:@selector(submitAnswer:)”

我希望能够在“submit Answer”方法中访问字符串“ CorrectAnswerString”,并且比较是否按下了具有正确答案的按钮。

我相信这是通过在 .h 文件中创建“@interface”来完成的,但我不知道该怎么做。

非常感谢您的帮助。

代码如下:


- (void) loadAnswerChoice
{

    int correctAnswer = 11;

    int incorrectOne = 20;

    int incorrectTwo = 5;

    int incorrectThree = 8;

    correctAnswerString = [NSString stringWithFormat:@"%d", correctAnswer]

    [button1 setTitle:[NSString stringWithFormat:@"%d", incorrectOne] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

    [button2 setTitle:[NSString stringWithFormat:@"%d", correctAnswer] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

    [button3 setTitle:[NSString stringWithFormat:@"%d", incorrectTwo] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

    [button4 setTitle:[NSString stringWithFormat:@"%d", incorrectThree] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

}

- (IBAction)submitAnswer:sender
{

    NSString *answer = [sender titleLabel].text;


    /*
    if ([answer == correctAnswerStr]) {
        //do something
    }
    else 
    {
        //do something else
    }
    */


    [self performSelector:@selector(loadAnswerChoice) withObject:nil afterDelay:1];
}

I have 4 choices assigned to 4 buttons. One of the choices is correct and it is assigned to the string "correctAnswerString"

The 4 buttons call "action:@selector(submitAnswer:)"

I want to be able to access the string "correctAnswerString" in the "submit Answer" method and compare if the button with the correct answer has been pressed.

I believe this is done by creating an "@interface" in the .h file, but I don't know how to do it.

Many appreciations for the help.

The code is below:


- (void) loadAnswerChoice
{

    int correctAnswer = 11;

    int incorrectOne = 20;

    int incorrectTwo = 5;

    int incorrectThree = 8;

    correctAnswerString = [NSString stringWithFormat:@"%d", correctAnswer]

    [button1 setTitle:[NSString stringWithFormat:@"%d", incorrectOne] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

    [button2 setTitle:[NSString stringWithFormat:@"%d", correctAnswer] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

    [button3 setTitle:[NSString stringWithFormat:@"%d", incorrectTwo] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

    [button4 setTitle:[NSString stringWithFormat:@"%d", incorrectThree] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchUpInside];

}

- (IBAction)submitAnswer:sender
{

    NSString *answer = [sender titleLabel].text;


    /*
    if ([answer == correctAnswerStr]) {
        //do something
    }
    else 
    {
        //do something else
    }
    */


    [self performSelector:@selector(loadAnswerChoice) withObject:nil afterDelay:1];
}

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

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

发布评论

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

评论(1

病毒体 2024-12-26 14:27:38

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

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

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

@interface MyClass

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

然后您将在您的 .m 中实现

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.

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;

Then you would implement in your .m

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文