簡単な問題を解決するのを手伝ってください。私はobjective-cの初心者で、javaからobjective-cに切り替えたばかりです。私はJavaフェアをよく知っていますが、それほど深くはありません。
私はiPhoneアプリを構築しています。私のアプリはとてもシンプルです。私のiphoneアプリの目的は、レストランで私のiphoneアプリを注文することです。
私のアプリの進捗状況: 私のアプリには現在、いくつかのviewPanelsとボタンしかありません:)これ が私のアプリのソースコード、firstviewスクリーンショットとsecondviewスクリーンショットです
問題: [コーヒー]ボタンをクリックすると、textFieldにコーヒーの名前とコーヒーの価格が表示されません。これは「コーヒー1」と表示されるはずです。そしてxcodeは私をiphonesimilatorからデバッガーに連れて行きます。コーヒーボタンを押すと、xcodeがデバッガーに移動します。
これがコーヒーボタンのアクションコードです
- (IBAction)Coffee:(id)sender {
int price = 1;
NSString *name = @"coffee";
Storage *order = [[Storage alloc] init];
[order setName:name]; // i assume the program crush at here when it look into setName method.
[order setPrice:price];
[orders addOrders:order];
// Sanity check: // the program not even hit this line yet before crush;
NSLog(@"There are now %d orders in the array.", [orders.getOrders count]);
for (Storage *obj in orders.getOrders){
[check setText:[NSString stringWithFormat:@"%@",[obj description]]]; // check is the TextField instant varible. and the description method is from storage.m to print out the name and price.
}
}
以下のコードは、顧客が注文したすべてのアイテムを格納するストレージクラスです。これは2次元配列であり、MyStoragesクラスはStorageクラスのラッパークラスです。配列形式は次のようになります。
arrayindex1->名前、価格
arrayindex2->名前、価格
arrayindex3->名前、価格
arrayindex4->名前、価格
Storage.h
#import <Foundation/Foundation.h>
@interface Storage : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger price;
@end
Storage.m
#import "Storage.h"
@implementation Storage
@synthesize name; // program crush and goes to here.
@synthesize price;
- (NSString *)description {
// example: "coffee 1"
return [NSString stringWithFormat:@"%@ %d", self.name, self.price];
}
@end
Storages.h
#import <Foundation/Foundation.h>
#import "Storage.h"
@interface Storages : NSObject
@property (nonatomic,strong, readwrite) NSMutableArray *orders;
-(void) addOrders:(Storage *)anOrder;
-(NSMutableArray *) getOrders;
@end
Storages.m
#import "Storages.h"
@implementation Storages
@synthesize orders;
- (id)init {
self = [super init];
if (self) {
orders = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addOrders:(Storage *)anOrder{
[orders addObject: anOrder];
}
-(NSMutableArray *) getOrders{
return orders;
}
@end