半手工打造一個Viewer (配合NIB)

本文介绍了一个简单的iOS应用——温度转换器的实现过程。该应用使用Objective-C编写,通过UIViewController来展示用户界面,并实现了从华氏度到摄氏度的转换功能。文章详细展示了如何创建视图控制器、配置用户界面元素以及实现转换逻辑。

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

[code]
//
// main.m
//
//
// Created by unknown on 12/6/1.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

// 好用的列按鈕項目建立巨集
#define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] \
initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self \
action:SELECTOR]

//Step 5.a 設計一個ViewController
@interface convertController:UIViewController
{

UITextField *field1;
UITextField *field2;
}
-(IBAction)doConvert:(id)sender;
@end

@implementation convertController
//Step 5.b 複寫loadView來佈置此ViewController的layerOut
-(void)loadView
{
// 建立視圖
self.view=[[[NSBundle mainBundle]loadNibNamed:@"View" owner:self options:NULL] lastObject];

//映射到HelloWorldController的item
field1=(UITextField *)[self.view viewWithTag:11];
field2=(UITextField *)[self.view viewWithTag:12];

[field1 setText:@"100"];

UIBarButtonItem *convertButtonItem =[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doConvert:)] autorelease];

self.navigationItem.rightBarButtonItem=convertButtonItem;

[self setTitle:@"Converter"];


}
-(IBAction)doConvert:(id)sender
{
float invalue = [[field1 text] floatValue];
float outvalue = (invalue - 32.0f) * 5.0f / 9.0f;
[field2 setText:[NSString stringWithFormat:@"%3.2f", outvalue]];
[field1 resignFirstResponder];
}
// 應用程式支援所有裝置擺設方向
- (BOOL) shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
@end


//=====================
//Step 1.建立UIApplicationDelegaye class
@interface converDelegate :NSObject<UIApplicationDelegate>
{
//Step 2.加入window 指標
UIWindow *_window;
}

@end

@implementation converDelegate
//Step 3.複寫applicationDidFinishLaunching
-(void)applicationDidFinishLaunching:(UIApplication *)application
{
//Step 4.構建window,並且保留在_window
_window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//Step 5.建構一個UINavigationController,並且初始化一個converController 物件當做RootViewController
UINavigationController *nc=[[UINavigationController alloc]initWithRootViewController:[[convertController alloc]init]];
//Step 6.把nc挂入到window的Root
[_window setRootViewController:nc];
//Step 7.show window
[_window makeKeyAndVisible];
}

@end


int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"converDelegate");
}
}

[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值