服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现简单计算器功能

iOS实现简单计算器功能

2022-07-29 11:00祝小言 IOS

这篇文章主要为大家详细介绍了iOS实现简单计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS实现简单计算器功能的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//  ZYAppDelegate.m
//  Calculator
//
//  Created by mac on 15-7-30.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//
 
#import "ZYAppDelegate.h"
 
@implementation ZYAppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    show=[[UITextField alloc] initWithFrame:CGRectMake(20, 30, 260, 30)];
    show.borderStyle=UITextBorderStyleRoundedRect;
    show.text=@"0";
    
    show.backgroundColor=[UIColor redColor];
    [self.window addSubview:show];
    
    
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame=CGRectMake(20, 80, 40, 40);
    [btn1 setTitle:@"1" forState:UIControlStateNormal];
    btn1.backgroundColor=[UIColor blueColor];
    btn1.tag=1;
    [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn1];
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame=CGRectMake(80, 80, 40, 40);
    [btn2 setTitle:@"2" forState:UIControlStateNormal];
    btn2.backgroundColor=[UIColor blueColor];
    btn2.tag=2;
    [btn2 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn2];
    
    UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame=CGRectMake(140, 80, 40, 40);
    [btn3 setTitle:@"3" forState:UIControlStateNormal];
    btn3.backgroundColor=[UIColor blueColor];
    btn3.tag=3;
    [btn3 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn3];
    
  
 
    
    UIButton *btn4=[UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame=CGRectMake(20, 140, 40, 40);
    [btn4 setTitle:@"4" forState:UIControlStateNormal];
    btn4.backgroundColor=[UIColor blueColor];
    btn4.tag=4;
    [btn4 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn4];
    
    UIButton *btn5=[UIButton buttonWithType:UIButtonTypeCustom];
    btn5.frame=CGRectMake(80, 140, 40, 40);
    [btn5 setTitle:@"5" forState:UIControlStateNormal];
    btn5.backgroundColor=[UIColor blueColor];
    btn5.tag=5;
    [btn5 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn5];
    
    UIButton *btn6=[UIButton buttonWithType:UIButtonTypeCustom];
    btn6.frame=CGRectMake(140, 140, 40, 40);
    [btn6 setTitle:@"6" forState:UIControlStateNormal];
    btn6.backgroundColor=[UIColor blueColor];
    btn6.tag=6;
    [btn6 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn6];
    
 
    
    
    UIButton *btn7=[UIButton buttonWithType:UIButtonTypeCustom];
    btn7.frame=CGRectMake(20, 200, 40, 40);
    [btn7 setTitle:@"7" forState:UIControlStateNormal];
    btn7.backgroundColor=[UIColor blueColor];
    btn7.tag=7;
    [btn7 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn7];
    
    UIButton *btn8=[UIButton buttonWithType:UIButtonTypeCustom];
    btn8.frame=CGRectMake(80, 200, 40, 40);
    [btn8 setTitle:@"8" forState:UIControlStateNormal];
    btn8.backgroundColor=[UIColor blueColor];
    btn8.tag=8;
    [btn8 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn8];
    
    UIButton *btn9=[UIButton buttonWithType:UIButtonTypeCustom];
    btn9.frame=CGRectMake(140, 200, 40, 40);
    [btn9 setTitle:@"9" forState:UIControlStateNormal];
    btn9.backgroundColor=[UIColor blueColor];
    
    btn9.tag=9;
    [btn9 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn9];
    
    
 
    
 
    UIButton *btn0=[UIButton buttonWithType:UIButtonTypeCustom];
    btn0.frame=CGRectMake(20, 260, 100, 40);
    [btn0 setTitle:@"0" forState:UIControlStateNormal];
    btn0.backgroundColor=[UIColor blueColor];
    btn0.tag=10;
    [btn0 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn0];
    
    
    
    
    
    UIButton *jia=[UIButton buttonWithType:UIButtonTypeCustom];
    jia.frame=CGRectMake(200, 140, 40, 40);
    [jia setTitle:@"+" forState:UIControlStateNormal];
    jia.backgroundColor=[UIColor blueColor];
    jia.tag=100101;
    [jia addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jia];
    
    
    UIButton *jian=[UIButton buttonWithType:UIButtonTypeCustom];
    jian.frame=CGRectMake(200, 200, 40, 40);
    [jian setTitle:@"-" forState:UIControlStateNormal];
    jian.backgroundColor=[UIColor blueColor];
    jian.tag=100102;
    [jian addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jian];
    
    
    
    UIButton *cheng=[UIButton buttonWithType:UIButtonTypeCustom];
    cheng.frame=CGRectMake(200, 80, 40, 40);
    [cheng setTitle:@"*" forState:UIControlStateNormal];
    cheng.backgroundColor=[UIColor blueColor];
    cheng.tag=100103;
    [cheng addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:cheng];
    
    
    UIButton *chu=[UIButton buttonWithType:UIButtonTypeCustom];
    chu.frame=CGRectMake(200, 260, 40, 40);
    [chu setTitle:@"/" forState:UIControlStateNormal];
    chu.backgroundColor=[UIColor blueColor];
    chu.tag=100104;
    [chu addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:chu];
  
 
    UIButton *deng=[UIButton buttonWithType:UIButtonTypeCustom];
    deng.frame=CGRectMake(140, 260, 40, 40);
    [deng setTitle:@"=" forState:UIControlStateNormal];
    deng.backgroundColor=[UIColor blueColor];
    deng.tag=100105;
    [deng addTarget:self action:@selector(equal) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:deng];
 
    return YES;
}
 
-(void)onClick:(UIButton *)btn{
 
    switch (btn.tag) {
        case 1:
            num1=@"1";
            show.text=num1;
            a=[num1 intValue];
            
            break;
        case 2:
            num1=@"2";
            show.text=num1;
            a=[num1 intValue];
            break;
        
        case 3:
            num1=@"3";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 4:
            num1=@"4";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 5:
            num1=@"5";
            show.text=num1;
            a=[num1 intValue];
 
            break;
        case 6:
            num1=@"6";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 7:
            num1=@"7";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 8:
            num1=@"8";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 9:
            num1=@"9";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 10:
            num1=@"0";
            show.text=num1;
            a=[num1 intValue];
 
            break;
        default:
            break;
    }
 
}
-(void)click:(UIButton *)btn{
    switch (btn.tag) {
        case 100101:
            b=1;
            c=a;
            break;
        case 100102:
            b=2;
            c=a;
            break;
        case 100103:
            b=3;
            c=a;
            break;
        case 100104:
            b=4;
            c=a;
            break;
       
    }
    
}
-(void)equal{
 
    if (b==1) {
        show.text=[NSString stringWithFormat:@"%d",(c+a)];
    }
    if (b==2) {
        show.text=[NSString stringWithFormat:@"%d",(c-a)];
    }
    if (b==3) {
        show.text=[NSString stringWithFormat:@"%d",(c*a)];
    }
    if (b==4) {
        show.text=[NSString stringWithFormat:@"%d",(c/a)];
    }
 
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.window endEditing:YES];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
 
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
 
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
 
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
 
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
 
@end

效果图:

iOS实现简单计算器功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/swanzhu/article/details/47155939

延伸 · 阅读

精彩推荐
  • IOSIOS中Json解析实例方法详解(四种方法)

    IOS中Json解析实例方法详解(四种方法)

    本文将介绍TouchJson、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法,解析国家气象局API。通过本文给大家介绍IOS中Json解析的四种方法,非常不错,具有参考...

    enuola11682021-01-20
  • IOS推荐一个非常棒的Titanium MVC框架

    推荐一个非常棒的Titanium MVC框架

    ChariTi是一个非常不错的Titanium MVC框架。此框架的特点是很多东西可直接通过配置文件实现,如APP的主题颜色,每个tab的内容,是否使用滑动菜单布局,是否...

    IOS开发网4672020-12-15
  • IOSiOS实现UITableView数据为空时的提示页面

    iOS实现UITableView数据为空时的提示页面

    最近工作中遇到一个需求,当UITableView数据为空的时候,给出一个简单的提示页面,通过从网上查找解决的方法,发现了两种实现的方法,现在分享给大家...

    挨踢的苹果8242021-02-18
  • IOSiOS开发技巧之自定义相机

    iOS开发技巧之自定义相机

    这篇文章主要为大家详细介绍了iOS开发技巧之自定义相机,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    VKOOY6462021-05-24
  • IOSiOS监听手机锁屏状态

    iOS监听手机锁屏状态

    iPhone的锁屏监测分为两种方式监听,本文给大家介绍的非常详细,具体内容详情大家通过本文详细了解下吧...

    bowei-iOS10072021-03-15
  • IOS如何在iphone IOS设备上使用二维码

    如何在iphone IOS设备上使用二维码

    深度解析iPhone ios设备上使用二维码是本文要介绍的内容,二维码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据...

    mxABC3672020-12-23
  • IOS浅析iOS的Xcconfig

    浅析iOS的Xcconfig

    本篇文章介绍IOS中Xcconfig的相关知识内容,有兴趣的朋友学习下吧。...

    iOS开发网6142021-04-16
  • IOSXcode8以及iOS10适配等常见问题汇总(整理篇)

    Xcode8以及iOS10适配等常见问题汇总(整理篇)

    随着iOS 10的更新以及Xcdoe 8的更新出现了很多问题,今天小编抽时间给大家整理下我遇到的坑特此分享到脚本之家平台,供大家参考...

    吃屁的小栗子5522021-02-01