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;
DetailViewController.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
何らかの理由でラベルが更新されることはありません。
質問を長くしてすみません、非常に具体的にしようとしました。