2013-07-22 IOS-User Interface

本文介绍了一个简单的iOS应用程序,演示了如何使用UIKit中的基本组件如UILabel、UITextField和UIButton来实现一个加法计算器。从创建控件到事件处理,文章详细解释了iOS UI编程的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ios常用控件
熟悉事件处理机制
iOS动画效果
掌握不同ViewController的使用场景,使用方法
iOS的各种设计模式
iOS硬件特性,会硬件编程
网络应用开发,数据持久化,多线程开发
 

UI加法运算
 UITextFiled,常用于显示少量信息的空间
text
textcolor
 


//

//  AppDelegate.h

//  UI_1

//

//  Created by 0101 on 13-7-22.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>

{

    UILabel *label1;

    UITextField *tf1;

    UITextField *tf2;

    UIButton *btn;

    UILabel *result;

    UIWindow *_window;

}

@property (retainnonatomicUIWindow *window;


@end


.m文件 


     CGRect r = CGRectMake(00320508);//创建一个矩形

    

    //属性的赋值方式

    //方式一

     self.window = [[[UIWindow alloc]initWithFrame:r]autorelease];

     self.window.backgroundColor = [UIColor redColor];

    

   //方式二

    UIWindow *aWin = [[UIWindow alloc]initWithFrame:r];//iOS不支持多窗口    iPhone4/4s 320*480像素 iPhone5s 320*508像素  一个App的高度44*44像素 aWin retainCount = 1  

    

    self.window = aWin;//aWin retainCount = 2

    aWin.backgroundColor = [UIColor grayColor];

    [aWin release];//aWin retainCount = 1,此时该 retainCount = 1Window,但是window的引用计数没有变化它还是1,此时只是控制权归window,单这并不代表它的引用计数归谁,拥有控制权就一定要释放。

    

    

    

    //Label 标签

    label1 = [[UILabel alloc]initWithFrame:CGRectMake(10904030)];

    label1.backgroundColor = [UIColor whiteColor];

    label1.text = @"";

    label1.textAlignment = NSTextAlignmentLeft;

    [self.window addSubview:label1];

    [label1 release];

     

    

    //TextFiled 文本输入框 tf1

    tf1= [[UITextField alloc]initWithFrame:CGRectMake(10504030)];

    tf1.borderStyle = UITextBorderStyleLine;

    tf1.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;

    tf1.keyboardType = UIKeyboardTypeNumberPad;

    [self.window addSubview:tf1];

    [tf1 release];

    

    

    //加号label

    UILabel *plus = [[UILabel alloc]initWithFrame:CGRectMake(60502030)];

    plus.text = @"+";

    plus.backgroundColor = [UIColor clearColor];

    plus.textAlignment = NSTextAlignmentCenter;

    [self.window addSubview:plus];

    [plus release];

    

    //TextFiled 文本输入框 tf2

    tf2 = [[UITextField alloc]initWithFrame:CGRectMake(100504030)];;

    tf2.borderStyle = UITextBorderStyleLine;

    tf2.keyboardType = UIKeyboardTypeNumberPad;

    [self.window addSubview:tf2];

    [tf2 release];

    

    

    //等号

    UILabel *equal = [[UILabel alloc]initWithFrame:CGRectMake(150502030)];

    equal.backgroundColor = [UIColor clearColor];

    equal.text = @"=";

    equal.textAlignment = NSTextAlignmentCenter;

    [self.window addSubview:equal];

    [equal release];

   

    

   

    

    //计算结果

    result = [[UILabel alloc]initWithFrame:CGRectMake(200504030)];

    result.backgroundColor = [UIColor clearColor];

    result.textAlignment = NSTextAlignmentCenter;

    [self.window addSubview:result];

    [result release];

    

    //Button 按钮

    btn = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];

    btn.frame = CGRectMake(1013010030);

    [btn setTitle:@"计算" forState:(UIControlStateNormal)];

    [btn setTitle:@"[计算]" forState:UIControlStateHighlighted];

    btn.tintColor = [UIColor orangeColor];

    

    //给按钮添加事件

    [btn addTarget:self

            action:@selector(btnClick)

  forControlEvents:(UIControlEventTouchUpInside)];

    [self.window addSubview:btn];

    

    //给按钮添加图片

    [btn setBackgroundImage:[UIImage imageNamed:@"abc.png"]

                   forState:UIControlStateNormal];

    

    [self.window makeKeyAndVisible];

    return YES;



-(void)btnClick

{

    int sum = 0;

    sum = [tf1.text intValue] + [tf2.text intValue];

    result.text = [NSString stringWithFormat:@"%d",sum];

    

} 


UI编程步骤
1、将各种控件根据需要(是否要创建成全局变量)声明在.h文件中
2、@synthesize window = _window; 
3、创建window    CGRect r = CGRectMake(00320508);//创建一个矩形
4、将矩形r赋值给window属性  
     
//方式一      self.window = [[[UIWindow alloc]initWithFrame:r]autorelease];

   //方式二     UIWindow *aWin = [[UIWindow alloc]initWithFrame:r]; //aWin retainCount = 1  

    
/*
    iOS
不支持多窗口 
    iPhone4/4s 320*480像素 
    iPhone5 320*508  
    
一个App的高度44*44 

    */
 

    self.window = aWin;//aWin retainCount = 2

    aWin.backgroundColor = [UIColor grayColor];

    [aWin release];//aWin retainCount = 1,此时该 retainCount = 1Window,但是window的引用计数没有变化它还是1,只是控制权归window,但这并不代表它的引用计数归谁,拥有控制权就一定要释放。

5、根据各种需要编写控件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值