【问题标题】:@protocol with integers@protocol 带整数
【发布时间】:2012-12-15 23:20:32
【问题描述】:

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

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

抱歉问题问了这么久,尽量说的很具体。

【问题讨论】:

  • 投反对票,嗯?那么新年快乐:)

标签: objective-c delegates int protocols


【解决方案1】:

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

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

    *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. - 随您喜欢)

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

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

HTH

【讨论】:

  • 对不起。星星被强调为重点。不知道为什么它在问题创建中不起作用。我按照你说的对我的代码做了什么,但标签仍然说我只答对了 1 个问题,而不是 2 个或 3 个等......
  • 也这样做了。不过,不。
  • @Yuval Marcus 编辑您的问题并修复删除多余的“”字符。检查语句:*-(void)questionsCorrectHasChangedTo:(int)questionNumberChanged**DetailQuestionViewController *dqvc = [[DetailQuestionViewController alloc] init]; dqvc.delegate = self;* 使用额外的“”很难信任代码。
  • @YuvalMarcus - 星星不适用于强调代码片段。如果您现在将代码更改为递增 questionsCorrect,则标签上的值保持为 1,并且您会收到多个 NSLog 行,说明值为 1,则表明您有多个 DetailQuestionViewController 实例,每个实例都有自己的计数器- 每个都从零开始并递增一次。如果是这种情况,您需要有一个 shared 计数器,一种可能性是根据上面最后一段将它包含在您的 ScoreViewController 类中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多