B类NSArray返回NIL?

时间:2013-06-30 06:33:28

标签: ipad nsmutablearray nsarray

我只是想在这两个类之间传递一个NSArray,每次我在类B中NSLog它的值时它返回NIL。

为什么会这样,我如何让它显示到这个UITableView?

这是Class 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

Class A.m

#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 DataSource。然后将其返回到在A类中分配和初始化的UIPopOverController。

班级B.h

#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

班级B.m

#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

我自己经历过几次,当我知道它被分配到A类时,它似乎无法弄清楚为什么它等于B类中的NIL。 非常感谢任何帮助,我确实在Stack Overflow上发现了类似的问题,但它们似乎没有解决我的问题,也从未在返回的NIL值上找到一个。

1 个答案:

答案 0 :(得分:5)

看起来,每当初始化Class A的实例时,您都会创建Class B的新实例,然后立即向其问题列表询问新的A.但是,在A的实现中,您不会在-viewDidLoad之前设置问题列表,因为您不会在所有init方法中加载视图,因此不会很快调用它。因此,我认为当你初始化B时,A的问题列表将是nil,因此B的问题列表也是nil

相关问题