0

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

这是为什么,我怎样才能让它显示到这个 UITableView?

这里是A类

#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 类中分配和初始化的 UIPopOverController。

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

Bm级

#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 值上找到一个。

4

1 回答 1

5

看起来您Class A在初始化 的实例时创建了一个新实例Class B,然后您立即向该新 A 询问其问题列表。但是,在 A 的实现中,您不会设置该问题列表,直到您的-viewDidLoad,它不会很快被调用(因为您没有在所有 init 方法中加载视图)。因此,我认为 A 的问题列表将nil在您初始化 B 时出现,所以 B 的问题列表也是nil如此。

于 2013-06-30T06:45:52.907 回答