gpt4 book ai didi

objective-c - 将数据从一个类移动到另一个类 - 同一个应用程序

转载 作者:行者123 更新时间:2023-12-03 17:33:46 25 4
gpt4 key购买 nike

这里需要真正的帮助!!!

我正在尝试在第二类中使用在第一类中创建的数据。我已经搜索 Youtube 和 StackOverflow 超过 1 周了。每当我认为我已经很接近时,就会缺少一些我无法把握的元素。我的最新尝试来自于 2011 年发布的这个网站(在 Objective-c 类之间传递数据),当应用程序编译时,我看不到第二类中的数据。

更具体。我使用 2 个类,因为数据收集在用户选择的组(第 1 类)中,并将在屏幕上显示在第 2 类的表格中。有 6 个 NSMutable 数组从源传递到第二类中的 6 个不同的数组。我将继续尝试解决,但我需要帮助。

以下是我根据 2011 年文章设计的内容:

一级.h代码(部分):

#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import "ReportsOutput.h"

//@class AppDelegate;

@class ReportsOutput;




@interface ReportsClass : NSWindowController<NSApplicationDelegate,NSMenuDelegate,NSWindowDelegate>{

ReportsOutput *ro;


//Shared Data arrays

NSMutableArray *tblYrScott;
NSMutableArray *tblYrExt;
NSMutableArray *tblYrYear;
NSMutableArray *tblYrType;
NSMutableArray *tblYrPrice;
NSMutableArray *tblYrDescription;

...... added code

@property(nonatomic,retain)NSMutableArray *tblYrScott;
@property(nonatomic,retain)NSMutableArray *tblYrExt;
@property(nonatomic,retain)NSMutableArray *tblYrType;
@property(nonatomic,retain)NSMutableArray *tblYrYear;
@property(nonatomic,retain)NSMutableArray *tblYrPrice;
@property(nonatomic,retain)NSMutableArray *tblYrDescription;

一级 .m 代码:​​

- (IBAction)btnShowDataOutput:(id)sender {

//pass data from Reports Class to Report Output Class

ReportsOutput *objReportsOutput = [[ReportsOutput alloc]init];

[objReportsOutput.tblScott setArray: tblYrScott];

[objReportsOutput.tblExt setArray:tblYrDescription];

[objReportsOutput.tblYear setArray:tblYrYear];

[objReportsOutput.tblType setArray:tblYrType];

[objReportsOutput.tblDescription setArray:tblYrDescription];

// open Reports Output Window

if (ro == nil){

ro = [[ReportsOutput alloc] initWithWindowNibName:@"ReportsOutput"];

}

[ro showWindow:nil];
}

第二类.h代码:

#import <Cocoa/Cocoa.h>
#import "ReportsClass.h"


@interface ReportsOutput : NSWindowController{

//shared data arrays
NSMutableArray *tblScott;
NSMutableArray *tblExt;
NSMutableArray *tblYear;
NSMutableArray *tblType;
NSMutableArray *tblPrice;
NSMutableArray *tblDescription;


}


@property(nonatomic,strong) NSMutableArray *tblScott;
@property(nonatomic,strong) NSMutableArray *tblExt;
@property(nonatomic,strong) NSMutableArray *tblYear;
@property(nonatomic,strong) NSMutableArray *tblType;
@property(nonatomic,strong) NSMutableArray *tblPrice;
@property(nonatomic,strong) NSMutableArray *tblDescription;


@end

二级 .m 代码:​​

#import "ReportsOutput.h"
//#import "ReportsClass.h"

@interface ReportsOutput ()


@end

@implementation ReportsOutput

@synthesize tblScott;
@synthesize tblExt;
@synthesize tblType;
@synthesize tblPrice;
@synthesize tblYear;
@synthesize tblDescription;


- (void)windowDidLoad {
[super windowDidLoad];

}

-(void)awakeFromNib{

[self dataCheck];
}

-(void)dataCheck{

int a;

for (a=0; a<[self.tblScott count]; a++){


NSLog(@"@i,%d :%@: %@: %@: %@: %@: %@",a,[tblScott objectAtIndex:a],[tblExt objectAtIndex:a],[tblYear objectAtIndex:a],[tblType objectAtIndex:a],[tblPrice objectAtIndex:a],[tblDescription objectAtIndex:a]);

}
}

最佳答案

在 btnShowDataOutput 中,您创建了 objReportsOutput,但该对象会在方法结束后立即消失。您不需要创建 objReportsOutput。相反,直接在 ReportsOutput 窗口 Controller 上设置属性:

if (ro == nil){

ro = [[ReportsOutput alloc] initWithWindowNibName:@"ReportsOutput"];
}

ro.tblScott = self.tblYrScott
ro.tblExt = self.tblYrExt
ro.tblYear = self.tblYrYear
ro.tblType = self.tblYrType
ro.tblDescription = self.tblYrDescription

[ro showWindow:nil];

关于objective-c - 将数据从一个类移动到另一个类 - 同一个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29809674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com