簡體   English   中英

從目標C / iOS中的另一個視圖控制器訪問C數組

[英]Accessing C array from another view controller in objective C/iOS

我有一個C數組val[6] ,用於在iPad應用程序的Objective-C ViewController類(我們將其稱為ClassA)中存儲一組雙精度值。 我需要從另一個ViewController(我們將其稱為ClassB)訪問此數組。 顯然,我不允許將數組設置為@property ,所以我做了一個getter方法,該方法只在數組中獲取一個值:

-(double)getVal:(int)index{
    return val[index];
}

我在ClassB的for循環中的其他方法中調用它:

double newVal[6];
for(int i = 0; i < 6; i++)
    newVal[i] = [obj getVal:i];

其中obj是我實例化的ClassA類型的對象。 我知道Kinda笨拙,但我認為它會很好地達到其目的。 不幸的是, newVal[]被填充了0,我不知道為什么。 val[]在ClassA的.h文件中實例化,並在每次調用getter方法之前運行程序時都會調用的方法中進行填充:

-(IBAction)intervalButton:(id)sender{
    //sum the previous time values
    double sum = 0;
    for(int i = 0; i < count; i++)
        sum += val[i];
    //I cut some code out of this method before posting it, 
    //so the conditions under which val is populated might not 
    //make sense, but I really just wanted display how I was setting the values of val.
    if(count < 5){
        val[count] = number - sum;
        count++;
    }else
        val[count] = number - sum;
}

為什么我的ClassB中的數組沒有被填充? 這是范圍的問題(即,我的數組填充在該方法中但不在該方法的外部)還是我嘗試從ClassA獲取值的方式存在問題?

聽起來obj要么為nil,要么實際上尚未在其數組中放置任何值。 一會兒調試將很容易告訴您哪種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM