小小词典(二,导航栏及其按钮)

导航栏用来显示应用的标题。在xib文件中拖动一个Navigation Bar,用来显示应用标题。title改为:小小词典。颜色改为亮黄色。再添加一个Bar Button item用来表示是汉译英还是英译汉。将Bar Button item放到Navigation Bar的左上角。


将4个png图片拖入工程。英中.png(20x20) 英中@2x.png(40x40) 中英.png(20x20) 中英@2x.png(40x40) 

再将查询按钮图片,查询.png,查询@2x.png拖入工程。

Bar button item设置图片英中.png,按钮设置图片查询.png。


为bar button item selector 关联方法- (IBAction)pressExchange:(UIBarButtonItem *)sender;

定义BOOL 变量,表示从英文到中文还是从中文到英文。

修改源文件。

//
//  ViewController.m
//  TinyDictionary
//
//  Created by cloud on 13-3-26.
//  Copyright (c) 2013年 cloud. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_textField release];
    [_lblResult release];
    [super dealloc];
}
- (IBAction)pressExchange:(UIBarButtonItem *)sender {
    _chineseToEnglish=!_chineseToEnglish;
    if (_chineseToEnglish) {
        sender.image=[UIImage imageNamed:@"中英"];
    }
    else
    {
        sender.image=[UIImage imageNamed:@"英中"];
    }
}

- (IBAction)pressFind:(UIButton *)sender {
    if ([_textField.text length]==0) {
        return;
    }
    //GB2312字符集
    NSStringEncoding encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString *strURL=nil;
    if (_chineseToEnglish) {
        //编码中文url
        strURL=[[NSString stringWithFormat:@"http://api.liqwei.com/translate/?language=zh-CN|en&content=%@",_textField.text] stringByAddingPercentEscapesUsingEncoding:encode];
    }
    else
    {
        //编码中文url
        strURL=[[NSString stringWithFormat:@"http://api.liqwei.com/translate/?language=en|zh-CN&content=%@",_textField.text] stringByAddingPercentEscapesUsingEncoding:encode];
    }
    
    NSURL* url=[NSURL URLWithString:strURL];   
    NSError *err=nil;
    //以指定编码得到url给出的内容
    NSString *strResult=[NSString stringWithContentsOfURL:url encoding:encode error:err];
    if (err) {
        NSLog(@"error=%@",[err description]);
    }
    else
    {
        _lblResult.text=strResult;        
    }
}
@end

运行结果如下:


源码地址:http://download.csdn.net/detail/cloud95/5183617

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值