我正在尝试为 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
由于某种原因,标签永远不会更新。
很抱歉提出这么长的问题,试图非常具体。