将数据从 1 class 移动到另一个 - 同一个应用
Moving data from 1 class to another - same app
这里需要真正的帮助!!!
我正在尝试在第二个 class 中使用第一个 class 中创建的数据。我已经搜索 Youtube 和 Whosebug 超过 1 周了。每次我认为自己很接近时,都会缺少一些我无法把握的元素。我最近的尝试来自于 2011 年发布的这个网站(在 classes objective-c 之间传递数据),当应用程序编译时,我看不到第二个 class.[=15 中的数据=]
更具体。我正在使用 2 classes,因为数据是按用户选择的组(第一个 class)收集的,并将在第二个 class 中的 table 中显示在屏幕上].有 6 个 NSMutable 数组从源传递到第二个 class 中的 6 个不同。我会继续尝试解决,但我可以使用帮助。
这是我从 2011 年的文章中得出的结论:
第一个class.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;
第一class.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];
}
第二个class.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
第二个class.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 window 控制器上设置属性:
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];
这里需要真正的帮助!!!
我正在尝试在第二个 class 中使用第一个 class 中创建的数据。我已经搜索 Youtube 和 Whosebug 超过 1 周了。每次我认为自己很接近时,都会缺少一些我无法把握的元素。我最近的尝试来自于 2011 年发布的这个网站(在 classes objective-c 之间传递数据),当应用程序编译时,我看不到第二个 class.[=15 中的数据=]
更具体。我正在使用 2 classes,因为数据是按用户选择的组(第一个 class)收集的,并将在第二个 class 中的 table 中显示在屏幕上].有 6 个 NSMutable 数组从源传递到第二个 class 中的 6 个不同。我会继续尝试解决,但我可以使用帮助。
这是我从 2011 年的文章中得出的结论:
第一个class.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;
第一class.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];
}
第二个class.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
第二个class.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 window 控制器上设置属性:
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];