iphoneアプリの開発
電卓アプリを作っています。
今困っているのは
1.割り算で小数点以下の計算ができない。
2.3つ以上の計算が(2×3×4のような)足し算しかできない。
3.間違えて数値を入力した場合に使うバックスペース的なボタンの作り方。
です。
どれか一つでもいいのでアドバイスいただけたらありがたいです。
#import "myViewController.h"
@implementation myViewController
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
startInput = YES;
currentValue = 0;
}
-(IBAction)numberButtonPressed:(id)sender
{
UIButton *b = (UIButton *)sender;
if( startInput ){
// 最初の1桁目が0なら表示しない
if( b.tag == 0 ) return;
// 新しく表示する文字列を作成
label.text = [NSString stringWithFormat:@"%d", b.tag];
startInput = NO;
} else {
// すでに表示されている文字列に連結
label.text = [NSString stringWithFormat:@"%@%d", label.text, b.tag];
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
-(IBAction)equalButtonPressed:(id)sender
{
currentValue = sum;
sum = sum-sum;
// 直前に押された演算子で場合分け
if( operation == 0 ){
currentValue += [label.text intValue];
} else if( operation == 1 ){
currentValue -= [label.text intValue];
} else if( operation ==2){
currentValue *= [label.text intValue];
} else if (operation ==3){
currentValue /= [label.text intValue];
}
// 表示の更新
label.text = [NSString stringWithFormat:@"%d", currentValue];
startInput = YES;
label2.text =@"=";
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
-(IBAction)opButtonPressed:(id)sender
{ UIButton *b = (UIButton *)sender;
// 現在値の保存
if( operation == 0 ){
currentValue= [label.text intValue];
sum +=currentValue;
label.text =[NSString stringWithFormat:@"%d", sum];
}
// 演算の保存
operation = b.tag;
startInput = YES;
if( operation == 0 ){
label2.text =@"+";
}
if( operation == 1 ){
label2.text =@"-";
}
if( operation == 2 ){
label2.text =@"×";
}
if( operation == 3 ){
label2.text =@"÷";
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
-(IBAction)clearButtonPressed:(id)sender
{
label.text = @"0";
startInput = YES;
label2.text =@"";
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio play];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
お礼
コメント大変ありがとうございます!下記がソース全部なんです。なにせ、超初心者なもので、びっくりされるような部分多々有ると思います。下記を例にとって、もう少しアドバイスいただけましたら大変嬉しいです。どうぞ宜しくお願い致します! //******************* //DAViewController.h(Parent) //******************* #import <UIKit/UIKit.h> @interface DAViewController : UIViewController @end //******************* //DAViewController.m(Parent) //******************* #import "DAViewController.h" #import "DANextView.h" #import "DANextView2.h" @interface DAViewController () @end @implementation DAViewController -(IBAction)goToNextView:(id)sender { DANextView *nextView = [[DANextView alloc] initWithNibName:@"DANextView" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:nextView animated:YES]; } -(IBAction)goToNextView2:(id)sender { DANextView2 *nextView2 = [[DANextView2 alloc] initWithNibName:@"DANextView2" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:nextView2 animated:YES]; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)dealloc { [super dealloc]; } @end //******************* //DANextView.h(Child) //******************* #import <UIKit/UIKit.h> #import <MediaPlayer/MPVolumeView.h> #import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVAudioPlayer.h> #import <AVFoundation/AVFoundation.h> @interface DANextView3 : UIViewController <AVAudioPlayerDelegate> { AVAudioSession *audioSession; NSURL *soundFile; AVAudioPlayer *sound; NSTimer *myTicker; IBOutlet UISlider *volumeSlider; IBOutlet UISlider *mySlider; } @property (nonatomic ,retain) UISlider *volumeSlider; @property (nonatomic ,retain) AVAudioSession *audioSession; @end //******************* //DANextView.m(Child) //******************* #import "DANextView.h" #import "DAViewController.h" @interface DANextView () @end @implementation DANextView @synthesize volumeSlider; @synthesize audioSession; -(IBAction)playSound:(id)sender { AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback error:nil]; if(sound) [sound release]; soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp3"]]; sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil]; sound.delegate = self; sound.numberOfLoops = -1; [sound prepareToPlay]; [sound play]; } -(IBAction)restSound:(id)sender{ [sound stop]; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)dealloc { [soundFile release]; [sound release]; [super dealloc]; } @end
補足
コメント大変ありがとうございます!下記がソース全部なんです。なにせ、超初心者なもので、びっくりされるような部分多々有ると思います。下記を例にとって、もう少しアドバイスいただけましたら大変嬉しいです。どうぞ宜しくお願い致します! //******************* //DAViewController.h(Parent) //******************* #import <UIKit/UIKit.h> @interface DAViewController : UIViewController @end //******************* //DAViewController.m(Parent) //******************* #import "DAViewController.h" #import "DANextView.h" #import "DANextView2.h" @interface DAViewController () @end @implementation DAViewController -(IBAction)goToNextView:(id)sender { DANextView *nextView = [[DANextView alloc] initWithNibName:@"DANextView" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:nextView animated:YES]; } -(IBAction)goToNextView2:(id)sender { DANextView2 *nextView2 = [[DANextView2 alloc] initWithNibName:@"DANextView2" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:nextView2 animated:YES]; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)dealloc { [super dealloc]; } @end //******************* //DANextView.h(Child) //******************* #import <UIKit/UIKit.h> #import <MediaPlayer/MPVolumeView.h> #import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVAudioPlayer.h> #import <AVFoundation/AVFoundation.h> @interface DANextView3 : UIViewController <AVAudioPlayerDelegate> { AVAudioSession *audioSession; NSURL *soundFile; AVAudioPlayer *sound; NSTimer *myTicker; IBOutlet UISlider *volumeSlider; IBOutlet UISlider *mySlider; } @property (nonatomic ,retain) UISlider *volumeSlider; @property (nonatomic ,retain) AVAudioSession *audioSession; @end //******************* //DANextView.m(Child) //******************* #import "DANextView.h" #import "DAViewController.h" @interface DANextView () @end @implementation DANextView @synthesize volumeSlider; @synthesize audioSession; -(IBAction)playSound:(id)sender { AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback error:nil]; if(sound) [sound release]; soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"1" ofType:@"mp3"]]; sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil]; sound.delegate = self; sound.numberOfLoops = -1; [sound prepareToPlay]; [sound play]; } -(IBAction)restSound:(id)sender{ [sound stop]; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)dealloc { [soundFile release]; [sound release]; [super dealloc]; } @end