2013-06-30 44 views
0

我只是試圖在這兩個類之間傳遞一個NSArray,每當我在它的類B中NSLog的值返回NIL。B類中的NSArray返回NIL?

這是爲什麼,我怎麼才能讓它顯示到這個UITableView?

這裏是類A.H

#import <UIKit/UIKit.h> 
#import "Class B.h" 

@class Class B; 

@interface Class A : UIViewController <UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UIPopoverControllerDelegate> { 

    IBOutlet UITableView *ToolsTable; 
    IBOutlet UICollectionView *strategyCollection; 
    IBOutlet UIButton *countButton; 
    IBOutlet UIButton *camerButton; 
    IBOutlet UIButton *videoButton; 
    IBOutlet UIButton *textButton; 
    IBOutlet UIButton *probeButton; 
    IBOutlet STPopOverQuestionViewViewController *questionListController; 
    IBOutlet UIPopoverController *questionList; 
    NSMutableDictionary *stratToolsDict; 
    NSMutableArray *stratTools; 
    NSMutableArray *stratObjects; 
    NSMutableDictionary *QuestionDict; 
    NSMutableArray *QuestionsList; 
} 

@property (strong, nonatomic) IBOutlet UIView *strategyOBJView; 
@property (retain, nonatomic) NSMutableDictionary *stratToolsDict; 
@property (retain, nonatomic) NSMutableArray *stratTools; 
@property (retain, nonatomic) NSMutableArray *stratObjects; 
@property (retain, nonatomic) NSMutableDictionary *QuestionDict; 
@property (retain, nonatomic) NSMutableArray *QuestionsList; 

- (IBAction)questionListClick:(id)sender; 

@end 

類時三十分

#import "STQuestionViewViewController.h" 
#import "STQuestionCreatorViewController.h" 
#import "STPopOverQuestionViewViewController.h" 


@interface STQuestionViewViewController() { 
    NSMutableArray *toolsList; 
    NSArray *strategyData; 
    UIViewController *popOverQuestionView; 
    NSMutableDictionary *stratObjectsDict; 
} 


@end 

@implementation STQuestionViewViewController 
@synthesize QuestionDict; 
@synthesize stratToolsDict; 
@synthesize stratObjects; 
@synthesize stratTools; 
@synthesize QuestionsList; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     toolsList = [[NSMutableArray alloc] init]; 
     NSDictionary *count = [[NSDictionary alloc] initWithObjectsAndKeys:@"Count", @"title",nil]; 
     NSDictionary *camera = [[NSDictionary alloc] initWithObjectsAndKeys:@"Camera",@"title", nil]; 
     NSDictionary *video = [[NSDictionary alloc] initWithObjectsAndKeys:@"Video",@"title", nil]; 
     NSDictionary *text = [[NSDictionary alloc] initWithObjectsAndKeys:@"Text",@"title", nil]; 
     NSDictionary *probe = [[NSDictionary alloc] initWithObjectsAndKeys:@"Probe",@"title", nil]; 

     [toolsList addObject:count]; 
     [toolsList addObject:camera]; 
     [toolsList addObject:video]; 
     [toolsList addObject:text]; 
     [toolsList addObject:probe]; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Question Array Setup and Alloc 
    stratToolsDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:countButton,@"count",camerButton,@"camera",videoButton,@"video",textButton,@"text",probeButton,@"probe", nil]; 
    stratTools = [[NSMutableArray alloc] initWithObjects:@"Tools",stratToolsDict, nil]; 
    stratObjectsDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:stratTools,@"Strat1",stratTools,@"Strat2",stratTools,@"Strat3",stratTools,@"Strat4", nil]; 
    stratObjects = [[NSMutableArray alloc]initWithObjects:@"Strategies:",stratObjectsDict,nil]; 
    QuestionDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:stratObjects,@"Question 1?",stratObjects,@"Question 2?",stratObjects,@"Question 3?",stratObjects,@"Question 4?",stratObjects,@"Question 5?", nil]; 


    //add strategys to questions 
    QuestionsList = [[NSMutableArray alloc]init]; 
    for (int i = 0; i < 1; i++) { 
     [QuestionsList addObject:QuestionDict]; 
    } 
    NSLog(@"Object: %@",QuestionsList); 

* 我想傳遞的NSMutableArray QuestionsList;到B類並將其分配給UITableView數據源。隨後將其返回到分配和A類初始化

類了Bh

#import <UIKit/UIKit.h> 
#import "Class A.h" 

@class Class A; 

@interface Class B : UITableViewController<UITableViewDataSource, UITableViewDelegate> { 
    NSMutableDictionary *Questions; 
    NSMutableArray *QuestionList; 
    Class A *arrayQuestions; 
} 

@property Class A *arrayQuestions; 

@end 

類家蠶

#import "Class B.h" 
#import "Class A.h" 

@interface Class B() 

@end 

@implementation Class B 
@synthesize arrayQuestions; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    if ([super initWithStyle:style] != nil) { 

     //Make array 
     arrayQuestions = [[STQuestionViewViewController alloc]init]; 
     QuestionList = [arrayQuestions QuestionsList]; 

     //Log test 
     NSLog(@"QuestionList init method: %@",QuestionList); 
     NSLog(@"QuestionsDict init method: %@",Questions); 

     //Make row selections persist. 
     self.clearsSelectionOnViewWillAppear = NO; 

     //Calculate how tall the view should be by multiplying 
     //the individual row height by the total number of rows. 
     NSInteger rowsCount = [QuestionList count]; 
     NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView 
               heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
     NSInteger totalRowsHeight = rowsCount * singleRowHeight; 

     //Calculate how wide the view should be by finding how 
     //wide each string is expected to be 
     CGFloat largestLabelWidth = 0; 
     for (NSString *colorName in Questions) { 
      //Checks size of text using the default font for UITableViewCell's textLabel. 
      CGSize labelSize = [colorName sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]]; 
      if (labelSize.width > largestLabelWidth) { 
       largestLabelWidth = labelSize.width; 
      } 
     } 

     //Add a little padding to the width 
     CGFloat popoverWidth = largestLabelWidth + 100; 

     //Set the property to tell the popover container how big this view will be. 
     self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight); 
    } 

    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Log object 
    NSLog(@"View did load: %@",QuestionList); 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    //Log test 
    NSLog(@"QuestionList Sections Method: %@",QuestionList); 
    NSLog(@"QuestionsDict Sections Method: %@",Questions); 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [QuestionList count]; 
    //Log test 
    NSLog(@"QuestionList Row Method: %@",QuestionList); 
    NSLog(@"QuestionsDict Row Method: %@",Questions); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Log test 
    NSLog(@"QuestionList Cell for Row: %@",QuestionList); 
    NSLog(@"QuestionsDict Cell For Row: %@",Questions); 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = [QuestionList objectAtIndex:indexPath.row]; 

    return cell;} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *selectedName = [QuestionList objectAtIndex:indexPath.row]; 



    if ([selectedName isEqualToString:@"Question 1?"]){ 

    } 
    else if ([selectedName isEqualToString:@"Question 2?"]){ 

    } 
    else if ([selectedName isEqualToString:@"Question 3?"]){ 

    } 
    else if ([selectedName isEqualToString:@"Question 4?"]){ 

    } 
    else if ([selectedName isEqualToString:@"Question 5?"]){ 

    } 

} 

@end 

我走過了一個UIPopOverController它自己有幾次,似乎不知道爲什麼它等於無s B當我知道它被分配到A類。 任何幫助非常感謝,我確實發現堆棧溢出類似的問題,但他們似乎並沒有解決我的問題,從來沒有找到一個返回的NIL值。

+1

這是很多的代碼,它有幾個數組。哪一行代碼恰好是你意外得到'nil'的那一行? –

+0

在A類中,有一個NSMutableArray * QuestionsList,我在其中添加一個NSMutableDictionary。對所有的代碼抱歉,我試圖看看它,並不能解決問題,所以我想打開問題的範圍。 – Keeano

回答

5

當你初始化一個Class B的實例時,看起來你創建了一個新實例Class A,然後你立刻要求新的A作爲它的問題列表。但是,在A的實現中,直到您的-viewDidLoad不會被調用(因爲您沒有在所有init方法中加載視圖),您不會設置該問題列表。因此,當您初始化B時,我認爲A的問題列表將爲nil,因此B的問題列表也是nil

+0

我仍然有這個問題,以及試圖按照你的答覆。我應該如何以及在哪裏實現我的數組?所以我可以把它從A傳給Clas B? – Keeano

+0

那我應該在哪裏初始化它呢?當我嘗試在我的Init方法中初始化它時收到SIGABRT。 – Keeano