我有一個程序在兩個不同的窗口上有兩個表。一個表格包含客戶名稱和ID號碼,另一個表格包含物品名稱和編號。它們都存儲在一個數組中,也存儲在.plist文件中。如何從ID號碼獲取對象實例的信息
我想要做的是在第三頁上將出現一個銷售頁面,用戶將輸入客戶ID和項目ID,程序應該能夠找到名稱並將其顯示給標籤。我不知道從哪裏開始去。有人請幫忙或告訴我該怎麼辦?我可以上傳任何人想看到的代碼,但因爲我不知道從哪裏開始,所以我不知道要上傳什麼。
這是customer.h文件
#import <Foundation/Foundation.h>
NSString *name;
int memberNumber;
@interface Customer : NSObject <NSCoding>
{
NSString *name;
int memberNumber;
}
@property (nonatomic, copy) NSString *name;
@property int memberNumber;
@end
這是customer.m
#import "Customer.h"
@implementation Customer
@synthesize name;
@synthesize memberNumber;
-(id) init
{
self = [super init];
if(self)
{
name = @"Test";
int i = arc4random()%1000000000000000000;
if (i<0)
{
memberNumber = i*-1;
}
else
memberNumber = i;
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
self.name = [decoder decodeObjectForKey:@"name"];
self.memberNumber = [decoder decodeIntForKey:@"memberNumber"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:name forKey:@"name"];
[encoder encodeInt:memberNumber forKey:@"memberNumber"];
}
-(void)dealloc
{
[name release];
[super dealloc];
}
@end
這是tableView.h文件
#import <Foundation/Foundation.h>
#include <stdlib.h>
NSString *filepath;
@interface tableViewData : NSObject <NSTableViewDataSource>
{
@private
IBOutlet NSTableView *tableView;
NSMutableArray *list;
NSString *filepath;
}
-(IBAction)add:(id)sender;
-(IBAction)remove:(id)sender;
@end
這是tableView.m文件
#import "tableViewData.h"
#import "Customer.h"
@implementation tableViewData
-(void)awakeFromNib{
filepath = @"/Users/Desktop/CustomerNames.plist";
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath])
{
NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
list = archive;
}
else
list=[[NSMutableArray alloc]init];
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [list count];
}
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [Customer valueForKey:identifier];
}
-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn: (NSTableColumn *)tableColumn row:(NSInteger)row
{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
[Customer setValue:object forKey:identifier];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
}
-(IBAction)add:(id)sender
{
[list addObject:[[Customer alloc]init]];
[tableView reloadData];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
for (id name in list)
NSLog(@"obj: %@", name);
NSLog (@"array:%@",list);
}
-(IBAction)remove:(id)sender
{
NSInteger row = [tableView selectedRow];
if (row != -1)
{
[list removeObjectAtIndex:row];
}
[tableView reloadData];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
}
-(void)dealloc
{
[super dealloc];
}
@end
希望這有助於
(針對OS X的Xcode 4.2.1)
因此,如果我理解正確,讓我的程序顯示一個客戶的名稱只有一個ID號我會做你說的話,然後在方法位我會說[標籤setValue名稱]; ? – GBSingh 2012-04-13 08:31:50
等等......你是用id表示的? http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html對象標識符或您的internat客戶ID? – Kuba 2012-04-13 08:38:26
我的意思是給每位顧客一個ID號碼 – GBSingh 2012-04-13 08:45:53