2

我正在尝试为 tableView 的详细视图制定协议。详细视图有一个问题,然后是一个答案。如果我得到正确的答案,它将在协议方法中设置一个整数 1。

我是协议的新手,我不明白我做错了什么。

代码

DetailViewController.h

制定协议的地方

#import "Question.h"

@protocol DetailQuestionViewControllerDelegate <NSObject>
-(void)questionsCorrectHasChangedTo:(int)questionNumberChanged;
@end

@interface DetailQuestionViewController : UIViewController

@property (nonatomic, strong) Question *selectedQuestion;

@property (strong, nonatomic) IBOutlet UILabel *questionLabel;
@property (strong, nonatomic) IBOutlet UITextField *answerField;
@property (strong, nonatomic) IBOutlet UILabel *correctLabel;
@property (nonatomic,strong) id <DetailQuestionViewControllerDelegate> delegate;

@property (assign, nonatomic) int questionsCorrect;

细节视图控制器.m

@implementation DetailQuestionViewController

@synthesize questionLabel;
@synthesize answerField;
@synthesize correctLabel;
@synthesize selectedQuestion;
@synthesize questionsCorrect;
@synthesize delegate;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Sets the questionLabel to the question we put in the array
    self.questionLabel.text = [selectedQuestion questionName]; 

    // Sets the navigation title to the rowName we put in the array
    self.navigationItem.title = [selectedQuestion questionRowName]; 

   NSLog(@"The question's answer for the question you selected is %@", [selectedQuestion questionAnswer]);


}

- (IBAction)checkAnswer:(UITextField *)sender
{

    if ([[selectedQuestion questionAnswer] caseInsensitiveCompare:answerField.text] == NSOrderedSame)
    {
        // Show the  correct label
        [correctLabel setHidden:NO];
        correctLabel.text = @"Correct!";
        correctLabel.textColor = [UIColor greenColor];
        *questionsCorrect = 1;
        NSLog(@"questionsCorrect int is %d", questionsCorrect);
        [self.delegate questionsCorrectHasChangedTo:questionsCorrect];*

    }
        else
        {
            // Show the incorrect label
            [correctLabel setHidden:NO];
            correctLabel.text = @"Incorrect";
            correctLabel.textColor = [UIColor redColor];

        }

        // Erase the text in the answerField
        answerField.text = @"";
}

ScoreViewController.h

现在这是我的 ScoreView 将访问委托

#import <UIKit/UIKit.h>
#import "DetailQuestionViewController.h"

@interface ScoreViewController : UIViewController *<DetailQuestionViewControllerDelegate>*


@property (strong, nonatomic) IBOutlet UILabel *scoreLabel;

- (IBAction)resetButtonClicked:(UIButton *)sender;
-(void)checkScore;

@end

ScoreViewController.m

#import "ScoreViewController.h"
#import "DetailQuestionViewController.h"

@interface ScoreViewController ()

@end

@implementation ScoreViewController
@synthesize scoreLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];

    *DetailQuestionViewController *dqvc = [[DetailQuestionViewController alloc] init];
    dqvc.delegate = self;*

}

-(void)viewWillAppear:(BOOL)animated
{

    [self checkScore];

}

-(void)checkScore
{

}

- (IBAction)resetButtonClicked:(UIButton *)sender
{

}

#pragma mark - DetailQuestionViewControllerDelegate -
*-(void)questionsCorrectHasChangedTo:(int)questionNumberChanged*
{
    //set the textlabel text value to the number of questions correct
    NSLog(@"questionsNumberChanged is %i", questionNumberChanged);
    scoreLabel.text = [NSString stringWithFormat:@"You answered %d questions correctly",questionNumberChanged];
}

@end

由于某种原因,标签永远不会更新。

很抱歉提出这么长的问题,试图非常具体。

4

1 回答 1

1

当您谈论增加协议方法中的值时,我在这里有些猜测,但是您没有单个+++任何地方...您的示例代码中也有很多*' 散布在奇怪的地方,它是不清楚这些是拼写错误,是为了强调,还是为了指针间接。

所以你的类中有一个属性questionsCorrectDetailQuestionViewController所以让我们假设这是你希望拥有计数器的类(我们将跳过这是一个视图而不是模型类......)。如果这是这个想法,那么线路:

    *questionsCorrect = 1;
    NSLog(@"questionsCorrect int is %d", questionsCorrect);
    [self.delegate questionsCorrectHasChangedTo:questionsCorrect];*

应该:

    self.questionsCorrect++;  // increment the counter
    NSLog(@"questionsCorrect int is %d", self.questionsCorrect);
    [self.delegate questionsCorrectHasChangedTo:self.questionsCorrect];

(您也可以questionsCorrect自己声明实例变量并放弃使用self.上面的 - 无论您喜欢哪个)

现在只需检查并删除 extra 的其他情况(*如果它们在您的代码以及上面的示例中),您将更接近您的目标。

如果您希望ScoreViewController拥有计数器,那么您需要在此处声明它并提供一种方法来增加和显示它。

高温高压

于 2013-01-01T04:37:16.720 回答