iOSへの挑戦 その2 バーゲン教師編
こんばんは。frederickです。
ようやくバーゲン教師制作の報告ができる;
まだ半分くらい理解していないが、結果としては完成できました。
ダイアログのdestructiveButtonが押されたらcartPriceに0を表示するプログラムも完璧。
中身を理解するのも大切だけど、まずは分からないなりに作るだけ作ってみるのもいいと思う。
ソースコードをのせておくので、興味のある方はどうぞ。(プログラム名はBargainSaleFinalで作ってます。)
各パーツの配置と接続、OutletとActionの組み合わせは、
目指せ!iPhoneアプリ開発エキスパート 第8回 アプリの完成を目指して の1Pを見て設定してください。
ではまずは、BargainSaleFinalViewController.mのソースコード。
----------------------------------------------------------------------------------
@implementation BargainSaleFinalViewController
- (IBAction)buttonValueChanged:(id)sender {
//割引率を保持する変数
float discount_value = 0.2f;
//セグメンテッドコントロールの選択位置から割引率を決定
int selected_index = [discountButton selectedSegmentIndex];
switch(selected_index){
case 0:
discount_value = 0.2f;
break;
case 1:
discount_value = 0.3f;
break;
case 2:
discount_value = 0.4f;
break;
case 3:
discount_value = 0.5f;
break;
default:
break;
}
//選択されている割引率の位置にスライダーのつまみを動かす
[discountSlider setValue:discount_value animated:YES];
//割引率をパーセント形式でラベルに表示
[discountPercent setText:[NSString stringWithFormat:@"%d", (int)(discount_value * 100.f)]];
//元値と割引率から割引後の値段を計算してラベルに表示
int price_original = [[priceOriginal text] intValue];
if(price_original > 0){
[priceNew setText:[NSString stringWithFormat:@"%d", (int)(price_original * (1.f - discount_value))]];
}
}
- (IBAction)escapeButtonPush:(id)sender {
[priceOriginal endEditing:YES];
}
- (IBAction)pushAddCart:(id)sender {
int org = [[cartPrice text] intValue];
int add = [[priceNew text] intValue];
[cartPrice setText:[NSString stringWithFormat:@"%d", org + add]];
}
- (IBAction)pushClearCart:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"カートをクリアしますか?" delegate:self cancelButtonTitle:@"キャンセル" destructiveButtonTitle:@"クリアする" otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
//カートをクリアする
[cartPrice setText:[NSString stringWithFormat:@"%d", 0]];
}
}
- (IBAction)sliderValueChanged:(id)sender {
//スライダーの値から割引率を決定,0.5単位に切り捨てる
float discount_value = (int)([(UISlider*)sender value] * 20) / 20.f;
//セグメンテッドコントロールに一致する割引率があれば選択されている状態にする
if(discount_value == 0.2f){
[discountButton setSelectedSegmentIndex:0];
}
else if(discount_value == 0.3f){
[discountButton setSelectedSegmentIndex:1];
}
else if(discount_value == 0.4f){
[discountButton setSelectedSegmentIndex:2];
}
else if(discount_value == 0.5f){
[discountButton setSelectedSegmentIndex:3];
}
else{
[discountButton setSelectedSegmentIndex:-1];
}
//割引率をパーセント形式でラベルに表示
[discountPercent setText:[NSString stringWithFormat:@"%d", (int)(discount_value * 100.f)]];
//元値と割引率から割引後の値段を計算してラベルに表示
int price_original = [[priceOriginal text] intValue];
if(price_original > 0){
[priceNew setText:[NSString stringWithFormat:@"%d", (int)(price_original * (1.f - discount_value))]];
}
}
@end
----------------------------------------------------------------------------------
続いて、BargainSaleFinalViewController.h
デリゲートの設定をするのをお忘れなく。
----------------------------------------------------------------------------------
@interface BargainSaleFinalViewController : UIViewController <UIActionSheetDelegate> {
IBOutlet id cartPrice;
IBOutlet id discountButton;
IBOutlet id discountPercent;
IBOutlet id discountSlider;
IBOutlet id priceNew;
IBOutlet id priceOriginal;
}
- (IBAction)buttonValueChanged:(id)sender;
- (IBAction)escapeButtonPush:(id)sender;
- (IBAction)pushAddCart:(id)sender;
- (IBAction)pushClearCart:(id)sender;
- (IBAction)sliderValueChanged:(id)sender;
@end
----------------------------------------------------------------------------------
これコピペするだけで一応完成するはずです。
ぜひやってみて。
この先は、自作のアプリをデバイスで動かすために「iPhone Developer Program」への登録(有償)
が必要なので、オイラはまだまだそんな域ではないので、
「目指せ!iPhoneアプリ開発エキスパート」はいったん卒業。
次からはもう少しLVUPしてこの本を参考にがんばってみようかな↓
まだまだ分からないことだらけだけど、まったりじっくりがんばりますよーb
今日のところはジブリ映画でも見て寝ます。。。
ではまた~ノシ
ようやくバーゲン教師制作の報告ができる;
まだ半分くらい理解していないが、結果としては完成できました。
ダイアログのdestructiveButtonが押されたらcartPriceに0を表示するプログラムも完璧。
中身を理解するのも大切だけど、まずは分からないなりに作るだけ作ってみるのもいいと思う。
ソースコードをのせておくので、興味のある方はどうぞ。(プログラム名はBargainSaleFinalで作ってます。)
各パーツの配置と接続、OutletとActionの組み合わせは、
目指せ!iPhoneアプリ開発エキスパート 第8回 アプリの完成を目指して の1Pを見て設定してください。
ではまずは、BargainSaleFinalViewController.mのソースコード。
----------------------------------------------------------------------------------
@implementation BargainSaleFinalViewController
- (IBAction)buttonValueChanged:(id)sender {
//割引率を保持する変数
float discount_value = 0.2f;
//セグメンテッドコントロールの選択位置から割引率を決定
int selected_index = [discountButton selectedSegmentIndex];
switch(selected_index){
case 0:
discount_value = 0.2f;
break;
case 1:
discount_value = 0.3f;
break;
case 2:
discount_value = 0.4f;
break;
case 3:
discount_value = 0.5f;
break;
default:
break;
}
//選択されている割引率の位置にスライダーのつまみを動かす
[discountSlider setValue:discount_value animated:YES];
//割引率をパーセント形式でラベルに表示
[discountPercent setText:[NSString stringWithFormat:@"%d", (int)(discount_value * 100.f)]];
//元値と割引率から割引後の値段を計算してラベルに表示
int price_original = [[priceOriginal text] intValue];
if(price_original > 0){
[priceNew setText:[NSString stringWithFormat:@"%d", (int)(price_original * (1.f - discount_value))]];
}
}
- (IBAction)escapeButtonPush:(id)sender {
[priceOriginal endEditing:YES];
}
- (IBAction)pushAddCart:(id)sender {
int org = [[cartPrice text] intValue];
int add = [[priceNew text] intValue];
[cartPrice setText:[NSString stringWithFormat:@"%d", org + add]];
}
- (IBAction)pushClearCart:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"カートをクリアしますか?" delegate:self cancelButtonTitle:@"キャンセル" destructiveButtonTitle:@"クリアする" otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
//カートをクリアする
[cartPrice setText:[NSString stringWithFormat:@"%d", 0]];
}
}
- (IBAction)sliderValueChanged:(id)sender {
//スライダーの値から割引率を決定,0.5単位に切り捨てる
float discount_value = (int)([(UISlider*)sender value] * 20) / 20.f;
//セグメンテッドコントロールに一致する割引率があれば選択されている状態にする
if(discount_value == 0.2f){
[discountButton setSelectedSegmentIndex:0];
}
else if(discount_value == 0.3f){
[discountButton setSelectedSegmentIndex:1];
}
else if(discount_value == 0.4f){
[discountButton setSelectedSegmentIndex:2];
}
else if(discount_value == 0.5f){
[discountButton setSelectedSegmentIndex:3];
}
else{
[discountButton setSelectedSegmentIndex:-1];
}
//割引率をパーセント形式でラベルに表示
[discountPercent setText:[NSString stringWithFormat:@"%d", (int)(discount_value * 100.f)]];
//元値と割引率から割引後の値段を計算してラベルに表示
int price_original = [[priceOriginal text] intValue];
if(price_original > 0){
[priceNew setText:[NSString stringWithFormat:@"%d", (int)(price_original * (1.f - discount_value))]];
}
}
@end
----------------------------------------------------------------------------------
続いて、BargainSaleFinalViewController.h
デリゲートの設定をするのをお忘れなく。
----------------------------------------------------------------------------------
@interface BargainSaleFinalViewController : UIViewController <UIActionSheetDelegate> {
IBOutlet id cartPrice;
IBOutlet id discountButton;
IBOutlet id discountPercent;
IBOutlet id discountSlider;
IBOutlet id priceNew;
IBOutlet id priceOriginal;
}
- (IBAction)buttonValueChanged:(id)sender;
- (IBAction)escapeButtonPush:(id)sender;
- (IBAction)pushAddCart:(id)sender;
- (IBAction)pushClearCart:(id)sender;
- (IBAction)sliderValueChanged:(id)sender;
@end
----------------------------------------------------------------------------------
これコピペするだけで一応完成するはずです。
ぜひやってみて。
この先は、自作のアプリをデバイスで動かすために「iPhone Developer Program」への登録(有償)
が必要なので、オイラはまだまだそんな域ではないので、
「目指せ!iPhoneアプリ開発エキスパート」はいったん卒業。
次からはもう少しLVUPしてこの本を参考にがんばってみようかな↓
まだまだ分からないことだらけだけど、まったりじっくりがんばりますよーb
今日のところはジブリ映画でも見て寝ます。。。
ではまた~ノシ




































