---
title: "手工打造一個Viewer"
author: "luckfox"
date: "2012-06-01T15:27:40Z"
tags: []
categories: ["行业应用"]
slug: "viewer"
draft: false
lastmod: "2012-06-01T15:27:40Z"
id: 1546572
url: "https://luckfox.iteye.com/blog/1546572"
---

```


//
//  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
{
	CGRect appFrame=[[uiscreen mainscreen]applicationFrame];//
	
	self.view=[[uiview alloc]initWithFrame:appFrame];
	field1 = [[uitextfield alloc] initWithFrame:
              CGRectMake(185.0, 16.0, 97.0, 31.0)];
    field1.borderStyle = UITextBorderStyleRoundedRect;
    field1.keyboardType = UIKeyboardTypeDecimalPad;
    field1.clearButtonMode = UITextFieldViewModeAlways;
    
    field2 = [[uitextfield alloc] initWithFrame:
              CGRectMake(185.0, 72.0, 97.0, 31.0)];
    field2.borderStyle = UITextBorderStyleRoundedRect;
    field2.enabled = NO;
    
    UILabel *label1 = [[uilabel alloc] initWithFrame:
                       CGRectMake(95.0, 19.0, 82.0, 21.0)];
    label1.text = @"Fahrenheit";
    label1.textAlignment = UITextAlignmentLeft;
    label1.backgroundColor = [uicolor clearcolor];
    
    UILabel *label2 = [[uilabel alloc] initWithFrame:
                       CGRectMake(121.0, 77.0, 56.0, 21.0)];
    label2.text = @"Celsius";
    label2.textAlignment = UITextAlignmentLeft;
    label2.backgroundColor = [uicolor clearcolor];
    
    // 將各元件加入內容視圖裡
    [self.view addsubview:field1];
    [self.view addsubview:field2];
    [self.view addsubview:label1];
    [self.view addsubview:label2];
	
	// 設定標題文字，加入Convert按鈕
    self.title = @"Converter";
    self.navigationItem.rightBarButtonItem = BARBUTTON(@"Convert", @selector(doConvert:));
    

}
-(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];
}
@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");
	}
}


```