Objective c @带整数的协议
我正在尝试为tableView的详细视图制定协议。详细视图有一个问题,然后有一个答案。如果我得到正确的答案,它会在协议方法中将整数设置为增加1 我是新的协议,我不明白我做错了什么 代码 DetailViewController.h 协议是在哪里制定的 ScoreViewController.h 现在,这是我的ScoreView,它将访问代表 由于某种原因,标签从未更新Objective c @带整数的协议,objective-c,delegates,int,protocols,Objective C,Delegates,Int,Protocols,我正在尝试为tableView的详细视图制定协议。详细视图有一个问题,然后有一个答案。如果我得到正确的答案,它会在协议方法中将整数设置为增加1 我是新的协议,我不明白我做错了什么 代码 DetailViewController.h 协议是在哪里制定的 ScoreViewController.h 现在,这是我的ScoreView,它将访问代表 由于某种原因,标签从未更新 很抱歉把问题问得这么长,我尽量说得非常具体。我在这里猜测一下,当你谈到在协议方法中增加一个值时,你没有一个+或+任何地方。。。您
很抱歉把问题问得这么长,我尽量说得非常具体。我在这里猜测一下,当你谈到在协议方法中增加一个值时,你没有一个
+
或+
任何地方。。。您的示例代码中还有相当多的*
散布在奇怪的地方,不清楚这些是打字错误,是作为强调还是作为指针间接指向
因此,您的DetailQuestionViewController
类中有一个属性QuestionsRect
,因此让我们假设这是您希望拥有计数器的类(我们将跳过这是一个视图而不是模型类…)。如果这是一个想法,那么:
*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];
(您还可以自己声明实例变量questionscorect
并放弃上面的self.
选项,以您喜欢的为准)
现在只需检查并删除额外的*
,如果它们在您的代码和上面的示例中,您将更接近您的目标
如果您希望ScoreViewController
拥有计数器,则需要在那里声明计数器,并提供递增和显示计数器的方法
HTH否决票,嗯?那么新年快乐:)对不起。明星们被认为是重点。不知道为什么它在问题创建中不起作用。我按照你说的做了我的代码,但标签上仍然说我只答对了1个问题,不是2个或3个……我也这么做了。当然,没有。@Yuval Marcus编辑您的问题并修复删除多余的“”字符。检查语句:
*-(void)questionsRectHasChanged到:(int)questionNumber更改*
和*DetailQuestionViewController*dqvc=[[DetailQuestionViewController alloc]init];dqvc.delegate=self*代码>有了额外的“s”,很难相信代码。@YuvalMarcus-stars在代码片段中不强调重点。如果您现在已将代码更改为incrementQuestionsRect
,则该值在标签上保持为1,并且您会得到多行NSLog
行,说明该值为1,这表明您有多个DetailQuestionViewController
实例,每个实例都有自己的计数器-每个实例从零开始并递增一次。如果这种情况下,您需要一个共享计数器,一种可能是按照上面最后一段将其包含在ScoreViewController
类中。
#import <UIKit/UIKit.h>
#import "DetailQuestionViewController.h"
@interface ScoreViewController : UIViewController *<DetailQuestionViewControllerDelegate>*
@property (strong, nonatomic) IBOutlet UILabel *scoreLabel;
- (IBAction)resetButtonClicked:(UIButton *)sender;
-(void)checkScore;
@end
#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
*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];