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

[复制链接]
查看11 | 回复5 | 2021-1-27 06:46:33 | 显示全部楼层 |阅读模式
}
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
//Overridepointforcustomizationafterapplicationlaunch.
self.window.backgroundColor=[UIColorwhiteColor];
oneLabel=[[UILabelalloc]initWithFrame:CGRectMake(10,30,300,50)];
[oneLabelsetTextAlignment:UITextAlignmentRight];
oneLabel.text=@"";
[self.windowaddSubview:oneLabel];
[oneLabelrelease];
oneButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
oneButton.frame=CGRectMake(10,90,150,50);
[oneButtonsetTitle:@"Del"forState:UIControlStateNormal];
[oneButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
[oneButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[oneButtonaddTarget:selfaction:@selector(delete:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:oneButton];
twoButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
twoButton.frame=CGRectMake(165,90,145,50);
[twoButtonsetTitle:@"C"forState:UIControlStateNormal];
[twoButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
[twoButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[twoButtonaddTarget:selfaction:@selector(clear)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:twoButton];
sixButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
sixButton.frame=CGRectMake(235,150,65,65);
[sixButtonsetTitle:@"+"forState:UIControlStateNormal];
[sixButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[sixButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[sixButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:sixButton];
sevenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
sevenButton.frame=CGRectMake(10,220,65,65);
[sevenButtonsetTitle:@"4"forState:UIControlStateNormal];
[sevenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[sevenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[sevenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:sevenButton];
eightButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
eightButton.frame=CGRectMake(85,220,65,65);
[eightButtonsetTitle:@"5"forState:UIControlStateNormal];
[eightButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[eightButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[eightButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:eightButton];
nineButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
nineButton.frame=CGRectMake(160,220,65,65);
[nineButtonsetTitle:@"6"forState:UIControlStateNormal];
[nineButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[nineButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[nineButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:nineButton];
tenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
tenButton.frame=CGRectMake(235,220,65,65);
[tenButtonsetTitle:@"-"forState:UIControlStateNormal];
[tenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[tenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[tenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:tenButton];
elevenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
elevenButton.frame=CGRectMake(10,290,65,65);
[elevenButtonsetTitle:@"7"forState:UIControlStateNormal];
[elevenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[elevenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[elevenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:elevenButton];
twelveButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
twelveButton.frame=CGRectMake(85,290,65,65);
[twelveButtonsetTitle:@"8"forState:UIControlStateNormal];
[twelveButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[twelveButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[twelveButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:twelveButton];
thirteenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
thirteenButton.frame=CGRectMake(160,290,65,65);
[thirteenButtonsetTitle:@"9"forState:UIControlStateNormal];
[thirteenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[thirteenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[thirteenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:thirteenButton];
fourteenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
fourteenButton.frame=CGRectMake(235,290,65,65);
[fourteenButtonsetTitle:@"*"forState:UIControlStateNormal];
[fourteenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[fourteenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[fourteenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:fourteenButton];

fifteenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
fifteenButton.frame=CGRectMake(10,360,65,65);
[fifteenButtonsetTitle:@"."forState:UIControlStateNormal];
[fifteenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[fifteenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[fifteenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:fifteenButton];
sixteenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
sixteenButton.frame=CGRectMake(85,360,65,65);
[sixteenButtonsetTitle:@"0"forState:UIControlStateNormal];
[sixteenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[sixteenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[sixteenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:sixteenButton];
seventeenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
seventeenButton.frame=CGRectMake(160,360,65,65);
[seventeenButtonsetTitle:@"="forState:UIControlStateNormal];
[seventeenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[seventeenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[seventeenButtonaddTarget:selfaction:@selector(equal:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:seventeenButton];
eighteenButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
eighteenButton.frame=CGRectMake(235,360,65,65);
[eighteenButtonsetTitle:@"/"forState:UIControlStateNormal];
[eighteenButtonsetTitleColor:[UIColorbrownColor]forState:UIControlStateNormal];
[eighteenButton.titleLabelsetFont:[UIFontfontWithName:@"Arial"size:20]];
[eighteenButtonaddTarget:selfaction:@selector(input:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:eighteenButton];
[self.windowmakeKeyAndVisible];
returnYES;
}
-(void)input:(id)sender
{
NSString*nstr;
UIButton*button=(UIButton*)sender;
if([oneLabel.textisEqual:@""]){
nstr=[[NSStringalloc]initWithFormat:@"%@",button.titleLabel.text];
}
else{
nstr=[oneLabel.textstringByAppendingString:button.titleLabel.text];
}
oneLabel.text=nstr;
}
-(void)clear
{
oneLabel.text=@"";
}
-(void)delete:(id)sender
{
NSString*nstr;
if(![oneLabel.textisEqual:@""]){
nstr=[oneLabel.textsubstringToIndex:[oneLabel.textlength]-1];
}
else{
nstr=@"";
}
oneLabel.text=nstr;
}



分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:46:33 | 显示全部楼层
叹为官职。。。不过貌似有个官方的计算器的例子,看了之后或许对你的东西有所帮助
回复

使用道具 举报

千问 | 2021-1-27 06:46:33 | 显示全部楼层
#import"MCCalculatorCenter.h"
@implementationMCCalculatorCenter
@synthesizeoperationStack=_operationStack;
-(void)pushOperand:(NSString*)operand
{
[self.operationStackaddObject:operand];//压入参数
}
-(NSMutableArray*)operationStack//初始化指针_operationStack若为空
{
if(_operationStack==nil)
{
_operationStack=[[NSMutableArrayalloc]init];
}
return_operationStack;
}
-(NSString*)popOperand
{
NSString*operand=[self.operationStacklastObject];
if(operand)
{
[self.operationStackremoveLastObject];//防止返回空值
}
returnoperand;
}
-(double)performOperation:(NSString*)opertion
{
doubleresult=0.0;
doublesecondOperand=[selfpopOperand].doubleValue;
NSString*waitingOperation=[selfpopOperand];
doublefirstOperand=[selfpopOperand].doubleValue;
if([waitingOperationisEqualToString:@"+"])
{
result=firstOperand+secondOperand;
}
elseif([waitingOperationisEqualToString:@"-"])
{
result=firstOperand-secondOperand;
}
elseif([waitingOperationisEqualToString:@"*"])
{
result=firstOperand*secondOperand;
}
elseif([waitingOperationisEqualToString:@"/"])
{
result=firstOperand/secondOperand;
}
else
{
result=secondOperand;//等号状态
}
[selfpushOperand:[NSStringstringWithFormat:@"%g",result]];
[selfpushOperand:opertion];
returnresult;
}
-(void)replaceStackTopOperation:(NSString*)operation
{
[selfpopOperand];
[selfpushOperand:operation];
}
-(void)CleanStack
{
[self.operationStackremoveAllObjects];
}
-(void)dealloc
{
[_operationStackrelease];
[superdealloc];
}
@end
回复

使用道具 举报

千问 | 2021-1-27 06:46:33 | 显示全部楼层
这个是怎么做的可以吧完整的文件发给我一下不我好好看看
回复

使用道具 举报

千问 | 2021-1-27 06:46:33 | 显示全部楼层
不错很好!谢谢分享!
回复

使用道具 举报

千问 | 2021-1-27 06:46:33 | 显示全部楼层
推荐使用autolayout来布局
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行