<span style="font-size:24px;">@interface FYZAppDelegate : UIResponder <UIApplicationDelegate, <span style="background-color: rgb(153, 153, 255);">UITextFieldDelegate</span>>
@property (retain, nonatomic) UIWindow *window;
@end
#import "FYZAppDelegate.h"
#define kBGView_Color [UIColor greenColor] //背景视图的颜色
#define kView_Color [UIColor whiteColor] //控件的背景颜色
#define kText_Color [UIColor brownColor] //文字的颜色
#define kMargin_Top 105
#define kBGView_Tag 101
#define kDisLabel_Tag 102
#define kTF_Tag 103
#define kLeftTF_Tag 104
#define kRightTF_Tag 105
#define kResultLabel_Tag 106
<span style="background-color: rgb(153, 153, 255);">@interface FYZAppDelegate ()
{
UIView *_displayView;
UIView *_calView;
}
@end</span>
@implementation FYZAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//containterView
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
bgView.tag = kBGView_Tag;
bgView.backgroundColor = kBGView_Color;
[self.window addSubview:bgView];
[bgView release];
//1.计算界面
_calView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
_calView.backgroundColor = kBGView_Color;
[bgView addSubview:_calView];
[_calView release];
//2.显示界面
_displayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
_displayView.backgroundColor = kBGView_Color;
[bgView addSubview:_displayView];
[_displayView release];
//3.完善计算界面
[self setupCalView];
//4.完善显示界面
[self setupDisplayView];
//<span style="background-color: rgb(153, 153, 255);">切换界面</span>
UIButton *exchangeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
exchangeBtn.frame = CGRectMake(50, 400, 220, 40);
exchangeBtn.backgroundColor = kView_Color;
exchangeBtn.layer.cornerRadius = 5;
[exchangeBtn setTitle:@"切换界面" forState:UIControlStateNormal];
[exchangeBtn addTarget:self action:@selector(exchangeView:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:exchangeBtn];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
<span style="background-color: rgb(153, 153, 255);">//当点击切换按钮时,交换计算界面和显示界面的位置</span>
- (void)exchangeView:(UIButton *)btn
{
[_calView.superview exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// [self.window.subviews objectAtIndex:0];
// [self.window viewWithTag:101];
}
- (void)setupCalView
{
//添加三个label,两个textField,一个button
//addLabel
UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, kMargin_Top, 20, 20)];
addLabel.textAlignment = NSTextAlignmentCenter;
addLabel.text = @"+";
addLabel.textColor = kText_Color;
addLabel.backgroundColor = [UIColor clearColor];
addLabel.font = [UIFont systemFontOfSize:20];
[_calView addSubview:addLabel];
[addLabel release];
//equalLabel
UILabel *equalLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, kMargin_Top, 20, 20)];
equalLabel.textAlignment = NSTextAlignmentCenter;
equalLabel.textColor = kText_Color;
equalLabel.text = @"=";
equalLabel.font = [UIFont systemFontOfSize:20];
[_calView addSubview:equalLabel];
[equalLabel release];
//resultLabel
UILabel *resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, kMargin_Top, 80, 30)];
resultLabel.textAlignment = NSTextAlignmentCenter;
resultLabel.textColor = kText_Color;
resultLabel.tag = kResultLabel_Tag;
resultLabel.backgroundColor = kView_Color;
resultLabel.font = [UIFont systemFontOfSize:20];
[_calView addSubview:resultLabel];
[resultLabel release];
//leftTF
UITextField *leftTF = [[UITextField alloc] initWithFrame:CGRectMake(30, kMargin_Top, 50, 30)];
leftTF.backgroundColor = kView_Color;
leftTF.tag = kLeftTF_Tag;
leftTF.placeholder = @"12";
leftTF.textAlignment = NSTextAlignmentCenter;
leftTF.keyboardType = UIKeyboardTypeNumberPad;
leftTF.clearsOnBeginEditing = YES;
[_calView addSubview:leftTF];
[leftTF release];
//rightTF
UITextField *rightTF = [[UITextField alloc] initWithFrame:CGRectMake(120, kMargin_Top, 50, 30)];
rightTF.textAlignment = NSTextAlignmentCenter;
rightTF.backgroundColor = kView_Color;
rightTF.tag = kRightTF_Tag;
rightTF.placeholder = @"23";
rightTF.keyboardType = UIKeyboardTypeNumberPad;
rightTF.clearsOnBeginEditing = YES;
[_calView addSubview:rightTF];
[rightTF release];
//calBtn
UIButton *calBtn = [UIButton buttonWithType:UIButtonTypeSystem];
calBtn.backgroundColor = kView_Color;
calBtn.frame = CGRectMake(50, 160, 220, 30);
calBtn.layer.cornerRadius = 5;
[calBtn setTitle:@"计算" forState:UIControlStateNormal];
[calBtn addTarget:self action:@selector(calculator:) forControlEvents:UIControlEventTouchUpInside];
[_calView addSubview:calBtn];
}
//当点击计算按钮时,将输入框中的数字相加求和之后,将结果用label显示
- (void)calculator:(UIButton *)btn
{
//1.获取输入框
UITextField *leftTF = (UITextField *)[_calView viewWithTag:kLeftTF_Tag];
UITextField *rightTF = (UITextField *)[_calView viewWithTag:kRightTF_Tag];
UILabel *resultLabel = (UILabel *)[_calView viewWithTag:kResultLabel_Tag];
//2.输入框中的文字,转换为基本数据类型,然后求和.
CGFloat result = [leftTF.text floatValue] + [rightTF.text floatValue];
resultLabel.text = [NSString stringWithFormat:@"%g", result];
//键盘回收
[rightTF resignFirstResponder];
[leftTF resignFirstResponder];
NSLog(@"计算");
}
- (void)setupDisplayView
{
//label
UILabel *disLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 220, 30)];
disLabel.backgroundColor = kView_Color;
disLabel.tag = kDisLabel_Tag;
disLabel.numberOfLines = 0;
disLabel.text = @"等待数据传入";
disLabel.textColor = kText_Color;
disLabel.textAlignment = NSTextAlignmentCenter;
disLabel.layer.cornerRadius = 4;
//将视图多余部分裁减掉
disLabel.layer.masksToBounds = YES;
[_displayView addSubview:disLabel];
[disLabel release];
//textField
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 130, 220, 40)];
tf.tag = kTF_Tag;
<span style="background-color: rgb(153, 153, 255);">//去掉输入框自动拼写检查</span>
tf.autocorrectionType = UITextAutocorrectionTypeNo;
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.placeholder = @"请输入.....";
tf.textColor = kText_Color;
tf.backgroundColor = kView_Color;
tf.clearsOnBeginEditing = YES;
tf.delegate = self;
[_displayView addSubview:tf];
[tf release];
//btn
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 200, 120, 30);
btn.backgroundColor = kView_Color;
btn.layer.cornerRadius = 4;
[btn setTitle:@"显示" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(display:) forControlEvents:UIControlEventTouchUpInside];
[_displayView addSubview:btn];
}
//当点击显示按钮时,输入框中的文字让label显示
- (void)display:(UIButton *)btn
{
//1.获取输入框
UITextField *tf = (UITextField *)[_displayView viewWithTag:kTF_Tag];
//2.获取label
UILabel *label = (UILabel *)[_displayView viewWithTag:kDisLabel_Tag];
//3.取出文字让label显示
label.text = tf.text;
//4.让输入框清空
tf.text = nil;
//5.让键盘回收
[tf resignFirstResponder];
NSLog(@"哭去吧");
}
<span style="background-color: rgb(153, 153, 255);">- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}</span>
</span>
显示传递,计算界面的切换
最新推荐文章于 2024-06-23 21:25:35 发布