Xcode写的一个UI计算器,加减乘除等号方法怎么实现,写不下来,中间删去了几个button

陆闪闪 2013-01-02 03:57:04

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

oneLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 50)];
[oneLabel setTextAlignment:UITextAlignmentRight];
oneLabel.text=@"";
[self.window addSubview:oneLabel];
[oneLabel release];
oneButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
oneButton.frame=CGRectMake(10, 90, 150, 50);
[oneButton setTitle:@"Del" forState:UIControlStateNormal];
[oneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[oneButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[oneButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:oneButton];
twoButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
twoButton.frame=CGRectMake(165, 90, 145,50 );
[twoButton setTitle:@"C" forState:UIControlStateNormal];
[twoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[twoButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[twoButton addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:twoButton];

sixButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
sixButton.frame=CGRectMake(235, 150, 65, 65);
[sixButton setTitle:@"+" forState:UIControlStateNormal];
[sixButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[sixButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[sixButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:sixButton];

sevenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
sevenButton.frame=CGRectMake(10, 220, 65, 65);
[sevenButton setTitle:@"4" forState:UIControlStateNormal];
[sevenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[sevenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[sevenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:sevenButton];

eightButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
eightButton.frame=CGRectMake(85, 220, 65, 65);
[eightButton setTitle:@"5" forState:UIControlStateNormal];
[eightButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[eightButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[eightButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:eightButton];

nineButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
nineButton.frame=CGRectMake(160, 220, 65, 65);
[nineButton setTitle:@"6" forState:UIControlStateNormal];
[nineButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[nineButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[nineButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:nineButton];

tenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
tenButton.frame=CGRectMake(235, 220, 65, 65);
[tenButton setTitle:@"-" forState:UIControlStateNormal];
[tenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[tenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[tenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:tenButton];

elevenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
elevenButton.frame=CGRectMake(10, 290, 65, 65);
[elevenButton setTitle:@"7" forState:UIControlStateNormal];
[elevenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[elevenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[elevenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:elevenButton];

twelveButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
twelveButton.frame=CGRectMake(85, 290, 65, 65);
[twelveButton setTitle:@"8" forState:UIControlStateNormal];
[twelveButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[twelveButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[twelveButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:twelveButton];

thirteenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
thirteenButton.frame=CGRectMake(160, 290, 65, 65);
[thirteenButton setTitle:@"9" forState:UIControlStateNormal];
[thirteenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[thirteenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[thirteenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:thirteenButton];

fourteenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
fourteenButton.frame=CGRectMake(235, 290, 65, 65);
[fourteenButton setTitle:@"*" forState:UIControlStateNormal];
[fourteenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[fourteenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[fourteenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:fourteenButton];


fifteenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
fifteenButton.frame=CGRectMake(10, 360, 65, 65);
[fifteenButton setTitle:@"." forState:UIControlStateNormal];
[fifteenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[fifteenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[fifteenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:fifteenButton];

sixteenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
sixteenButton.frame=CGRectMake(85, 360, 65, 65);
[sixteenButton setTitle:@"0" forState:UIControlStateNormal];
[sixteenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[sixteenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[sixteenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:sixteenButton];

seventeenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
seventeenButton.frame=CGRectMake(160, 360, 65, 65);
[seventeenButton setTitle:@"=" forState:UIControlStateNormal];
[seventeenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[seventeenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[seventeenButton addTarget:self action:@selector(equal:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:seventeenButton];

eighteenButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
eighteenButton.frame=CGRectMake(235, 360, 65, 65);
[eighteenButton setTitle:@"/" forState:UIControlStateNormal];
[eighteenButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[eighteenButton.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
[eighteenButton addTarget:self action:@selector(input:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:eighteenButton];
[self.window makeKeyAndVisible];
return YES;
}
-(void)input:(id)sender
{
NSString * nstr;
UIButton * button=(UIButton *)sender;
if ([oneLabel.text isEqual:@""]) {
nstr=[[NSString alloc]initWithFormat:@"%@",button.titleLabel.text];
}
else {
nstr=[oneLabel.text stringByAppendingString:button.titleLabel.text];
}
oneLabel.text=nstr;
}
-(void)clear
{
oneLabel.text=@"";
}
-(void)delete:(id)sender
{
NSString * nstr;
if (![oneLabel.text isEqual:@""]) {
nstr=[oneLabel.text substringToIndex:[oneLabel.text length]-1];
}
else {
nstr=@"";
}
oneLabel.text=nstr;
}


...全文
1315 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ynwlgh 2015-02-03
  • 打赏
  • 举报
回复
推荐使用autolayout 来布局
LcX_MoOd 2015-01-29
  • 打赏
  • 举报
回复
不错很好!谢谢分享!
Hidehowever 2013-10-22
  • 打赏
  • 举报
回复
这个是怎么做的 可以吧 完整的文件发给我一下不 我好好看看
huwuren1a 2013-01-17
  • 打赏
  • 举报
回复
#import "MCCalculatorCenter.h" @implementation MCCalculatorCenter @synthesize operationStack=_operationStack; -(void) pushOperand:(NSString *)operand { [self.operationStack addObject:operand]; //压入参数 } -(NSMutableArray*)operationStack //初始化指针_operationStack若为空 { if(_operationStack == nil) { _operationStack=[[NSMutableArray alloc] init]; } return _operationStack; } -(NSString*) popOperand { NSString *operand=[self.operationStack lastObject]; if(operand) { [self.operationStack removeLastObject]; //防止返回空值 } return operand; } -(double) performOperation:(NSString *)opertion { double result=0.0; double secondOperand=[self popOperand].doubleValue; NSString *waitingOperation=[self popOperand]; double firstOperand=[self popOperand].doubleValue; if([waitingOperation isEqualToString:@"+"]) { result=firstOperand+secondOperand; } else if([waitingOperation isEqualToString:@"-"]) { result=firstOperand-secondOperand; } else if([waitingOperation isEqualToString:@"*"]) { result=firstOperand*secondOperand; } else if([waitingOperation isEqualToString:@"/"]) { result=firstOperand/secondOperand; } else { result=secondOperand; //等号状态 } [self pushOperand:[NSString stringWithFormat:@"%g",result]]; [self pushOperand:opertion]; return result; } -(void) replaceStackTopOperation:(NSString *)operation { [self popOperand]; [self pushOperand:operation]; } -(void) CleanStack { [self.operationStack removeAllObjects]; } -(void)dealloc { [_operationStack release]; [super dealloc]; } @end
tfp 2013-01-06
  • 打赏
  • 举报
回复
叹为官职。。。 不过貌似有个官方的计算器的例子,看了之后或许对你的东西有所帮助

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧