iOS 在两个方法之间传递一个字符串 [英] iOS passing a string between two methods

查看:42
本文介绍了iOS 在两个方法之间传递一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 4 个选项分配给 4 个按钮.选项之一是正确的,它被分配给字符串correctAnswerString"

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

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

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

我希望能够在提交答案"方法中访问字符串correctAnswerString",并比较是否按下了具有正确答案的按钮.

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.

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

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

非常感谢您的帮助.

代码如下:

- (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];
}

推荐答案

点击没有用,只能再次比较.为什么不将 @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

这篇关于iOS 在两个方法之间传递一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆