2013-03-20 62 views
0

我真的無法弄清楚這一點。 我有一個名爲additionalMathViewController的VC向用戶顯示問題。如果用戶錯誤地回答,則用戶被引導到另一個VC,該VC是additionalMathViewController的子類,稱爲incorrectViewController。從子類訪問Root類中變量的值

#import "ViewController.h" 
@interface additionalmathViewController : ViewController{ 
int currentQuestion; 
} 

currentQuestion是一個計數器變量,它增加了所有用戶獲得問題的權利,並且可以顯示另一個問題。如果用戶得到錯誤的問題,我想去錯誤的ViewController。這是我的代碼

- (IBAction)SelectA:(id)sender { 
if ([self.correctAns isEqualToString:@"A"]) { 
     [self showNextQuestion]; 
} 
else{ 
    [self performSegueWithIdentifier: @"segueToIncorrect" sender:nil]; 
} 
} 

如果用戶選擇A並且它正確顯示另一個問題。如果錯誤執行Segue公司以incorrectViewController

#import "additionalmathViewController.h" 
@interface IncorrectImageViewController : additionalmathViewController 

在這裏,我想從additionalMathViewController

incorrectImage.image = [UIImage imageNamed:left[currentQuestion]]; 

此當前圖像抓取currentQuestion的價值不是來自additionalmathViewController

任何想法的價值?

+0

爲什麼你再問這個問題?您在一小時前發佈的其他問題中正確回答了這個問題。 Richard的答案與MikeD的答案相同。 – rdelmar 2013-03-20 18:12:52

+0

我以爲我的問題很模糊,我們沒有來答案,所以我認爲最好是重新發布更全面的展望 – Sfocker 2013-03-20 18:28:21

+0

嘗試下次修改您的當前答案,而不是發佈一個新的關於同一事物,但提出不同的詞。 – Petar 2013-03-20 20:05:56

回答

0

新的視圖控制器是一個新的實例,因此不知道以前的實例有什麼值。您需要通過prepareForSegue方法將您的currentQuestion值傳遞給新控制器。

製作currentQuestion屬性

@interface IncorrectImageViewController 
    @property (copy) NSInteger currentQuestion; 
.. 

然後在prepareForSegue設定的目標控制器上的值。

@implementation additionalmathViewController 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    IncorrectImageViewController *incorrect = [segue destinationViewController]; 
    incorrect.currentQuestion = self.currentQuestion; 
} 
+0

IncorrectImageViewController是子類的名稱>?是? – Sfocker 2013-03-20 18:25:08

+0

是的,它是子類。 – 2013-03-20 18:31:56

+0

在我IncorrectImageViewController我得到錯誤未聲明的標識符'currentQuestion' 在準備segue我需要命名segue嗎? – Sfocker 2013-03-20 20:47:38