五、点数器《苹果iOS实例编程入门教程》

时间:2023-03-10 02:36:24
五、点数器《苹果iOS实例编程入门教程》

该app为应用的功能为一个简单的数数程序

现版本 SDK 8.4 Xcode

运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 CountMeIn

(1)  在xCode打开 CountMeInViewController.h 文件,加入下面代码

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{

IBOutlet UILabel *counter;

}

-(IBAction)reset:(id)sender;

-(IBAction)addUint:(id)sender;

-(IBAction)subtractUint:(id)sender;

@end

(2)  在xCode打开 CountMeInViewController.m 文件,加入下面代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

int count = 0;

//清零

-(IBAction)reset:(id)sender

{

count = 0;

counter.text = @"0";

}

//增加 最大为999

-(IBAction)addUint:(id)sender

{

if(count >= 999)return;

NSString *numValue = [[NSString alloc]initWithFormat:@"%d",++count];

counter.text = numValue;

}

//减少 最小为0

-(IBAction)subtractUint:(id)sender

{

if(count <0)return;

NSString *numValue = [[NSString alloc]initWithFormat:@"%d",--count];

counter.text = numValue;

}

- (void)viewDidLoad {

counter.text = @"0";

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

(3) 导入下面图片文件

将下面的图片导入到项目文件夹Supporting Files中(此为原博客中图片)

backgroundPattern               IconGreenAdd    IconRedSubtract       

五、点数器《苹果iOS实例编程入门教程》    五、点数器《苹果iOS实例编程入门教程》             五、点数器《苹果iOS实例编程入门教程》

(4) UIView 界面设置

切换到main.storyboard

加入 Label ,显示程序计算结果

选择: Object Library 中拖拉一个 Label 到 Main View

鼠标右击Label控件,鼠标移动到"New Referencing Outlet" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "counter"; 选上它。

加入 Add Button , 进行累加计算

选择: Object Library 中拖拉一个 Button 到 Main View

鼠标右击Button控件,鼠标移动到"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "addUnit"; 选上它。

属性设置切换到Attributes上在 Type 下选择 custom; 在 Background 下选择 iconGreenAdd

加入 Subtract Button , 进行累减计算

选择: Object Library 中拖拉一个 Button 到 Main View

鼠标右击Button控件,鼠标移动到"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "SubtractUnit"; 选上它。

属性设置切换到Attributes上在 Type 下选择 custom; 在 Background 下选择 iconRedSubtract

加入 Reset Button , 进行清零

选择: Object Library 中拖拉一个 Button 到 Main View

鼠标右击Button控件,鼠标移动到"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"view controller";
放开鼠标选择键出现 "Reset"; 选上它。

属性设置切换到Attributes上在 Title 下填上 Reset

加入 UIimageView , 背景图案

选择: Object Library 中拖拉一个 imageView 到 Main View

点击 imageView 属性设置切换到Attributes上在 image下选择backgroundPattern.png

点击菜单栏的Editor->Arrange->Send to Back(如果以设置为背景或者没有选中imageView控件,Send to Back可能为灰色)

五、点数器《苹果iOS实例编程入门教程》

Size to Fit Content 这个按钮能够根据包含的图片或文本而调整大小了

选择: File -> Save

最后在 xCode 选择 Build and then Running

(5)模拟器效果图

五、点数器《苹果iOS实例编程入门教程》

本文源于网上博客教程,经过本人修改和测试。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100dx96.html