实例iPhone编程入门教程-第五天

DAY Five – Count Me In

今天给你的iPhone 制作一个简单的数数程序。

 纲要:

-在程序显示前运行代码;

-UIButton, UILabel, UIImageView 的运用;

-把integer转换为string;

        
首先运行以安装好的xCode

选择: File->New Project.

从 "New Project" 窗口
  
选择 : iPhone OS ->Applications-> View-Based Application
命名 : 我这里命名为 “CountMeIn”

 

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

#import <UIKit/UIKit.h>

 

@interface CountMeInViewController : UIViewController {

        IBOutlet UILabel *counter;

}

-(IBAction)reset;

-(IBAction)addUnit;

-(IBAction)subtractUnit;

 

@end

 

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

#import "CountMeInViewController.h"

 

@implementation CountMeInViewController

 

int count = 0;

 

-(IBAction)reset{

        count = 0;

        counter.text = @"0";

       

}

 

- (IBAction)addUnit {

       

        if(count >= 999) return;

       

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

        counter.text = numValue;

        [numValue release];

}

 

- (IBAction)subtractUnit {

       

        if(count <= 0) return;

       

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

        counter.text = numValue;

        [numValue release];    

}

// The designated initializer. Override to perform setup that is required before the view is loaded.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        // Custom initialization

    }

    return self;

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

        counter.text = @"0";

    [super viewDidLoad];

}

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview

    // Release anything that's not essential, such as cached data

}

 

 

- (void)dealloc {

    [super dealloc];

}

 @end

 

(3) 导入下面图片文件

 另存下面图片,放入countMeIn 文件夹内并命名为下面名称

backgroundPattern                                                                                   

                                                                                



  IconGreenAdd




IconRedSubtract


 

在xCode下右键点击CountMeIn->Add->Existing Files; 在countMeIn文件夹内,选择下载好的三个图片,按 Add

 

(4) UIView 界面设置

双击文件: "CountMeInViewController.xib" ;
然后 "Interface Builder"  会自动打开,在这里我们可以编辑改变界面
 

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

选择: Tools -> Library ;从Library显示菜单中拖拉一个 Label 到 Main View

在主视窗口或文件窗口;点击 Label

选择: Tools -> Connection Inspector

移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "counter"; 选上它。

(6) 加入 Add Button , 进行累加计算

选择: Tools -> Library ;从Library显示菜单中拖拉一个 Button 到 Main View

在主视窗口或文件窗口;点击 Button

选择: Tools -> Connection Inspector

移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "addUnit"; 选上它。

选择: Tools -> Attributes Inspector

在 Type 下选择 custom; 在 Background 下选择 iconGreenAdd

选择: Tools -> Size Inspector

调整 Size & Position

 

(7) 加入 Subtract Button , 进行累减计算

选择: Tools -> Library ;从Library显示菜单中拖拉一个 Button 到 Main View

在主视窗口或文件窗口;点击 Button

选择: Tools -> Connection Inspector

移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "SubtractUnit"; 选上它。

选择: Tools -> Attributes Inspector

在 Type 下选择 custom; 在 Background 下选择 iconRedSubtract

选择: Tools -> Size Inspector

调整 Size & Position

 

(8) 加入 Reset Button , 进行累减计算

选择: Tools -> Library ;从Library显示菜单中拖拉一个 Button 到 Main View

在主视窗口或文件窗口;点击 Button

选择: Tools -> Connection Inspector

移动鼠标在"Touch Up Inside" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "Reset"; 选上它。

选择: Tools -> Attributes Inspector

在 Title 下填上 Reset

 

(9) 加入 UIimageView , 背景图案

选择: Tools -> Library ;从Library显示菜单中拖拉一个 imageView 到 Main View; 调整到满屏

在主视窗口或文件窗口;点击 imageView

选择: Tools ->  Inspector; 在image下选择backgroundPattern.png

选择: Tools ->  Layout -> Send To Back;  图片设为背景

 

最后在 xCode 选择 Build->Build and Go; Save All.

 

下载今天程序文件: CountMeIn.zip


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值