从子类访问Root类中的变量的值 [英] Accessing the value of a variable in the Root class from a subclass
问题描述
我真的不能算出这个。
我有一个VC名为additionalMathViewController,向用户显示问题。如果用户回答不正确,则用户被引导到另一个VC,该VC是additionalMathViewController的子类,称为incorrectViewController。
I really can not figure this one out. I have a VC called additionalMathViewController which displays questions to the user. If the user answers incorrectly the user is directed to another VC which is a subclass of additionalMathViewController called incorrectViewController.
#import "ViewController.h"
@interface additionalmathViewController : ViewController{
int currentQuestion;
}
currentQuestion是一个计数器变量,增加用户获取问题的权利,问题可以显示。如果用户得到错误的问题,我想去wrongViewController。这是我的代码
currentQuestion is a counter variable which increases everything the user gets a question right and another question can be displayed. if the user get the question wrong I want to go to incorrectViewController. Here is my code
- (IBAction)SelectA:(id)sender {
if ([self.correctAns isEqualToString:@"A"]) {
[self showNextQuestion];
}
else{
[self performSegueWithIdentifier: @"segueToIncorrect" sender:nil];
}
}
如果用户选择A,题。如果它是错误的执行segue到incorrectViewController
If the user selects A and it is correct show another question. If it is wrong perform Segue to incorrectViewController
#import "additionalmathViewController.h"
@interface IncorrectImageViewController : additionalmathViewController
这里我想从additionalMathViewController获取currentQuestion的值
Here I want to grab the value of currentQuestion from additionalMathViewController
incorrectImage.image = [UIImage imageNamed:left[currentQuestion]];
此当前图像不是来自additionalmathViewController的值
This current image is not the value from the additionalmathViewController
任何想法?
推荐答案
新的视图控制器是一个新实例,因此不知道上一个实例为一个值。您需要在 prepareForSegue
方法中将您的currentQuestion值传递给新控制器。
The new view controller is a new instance, therefore has no idea what the previous instance had for a value. You need to pass your currentQuestion value to the new controller in prepareForSegue
method.
将currentQuestion作为属性
Make currentQuestion a property
@interface IncorrectImageViewController
@property (copy) NSInteger currentQuestion;
..
然后在prepareForSegue中设置目标控制器上的值。 p>
And then in the prepareForSegue set the value on the destination controller.
@implementation additionalmathViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
IncorrectImageViewController *incorrect = [segue destinationViewController];
incorrect.currentQuestion = self.currentQuestion;
}
这篇关于从子类访问Root类中的变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!