本文使用到的技术有:UINavigationController 的使用,对navigationBar 背景的替换,使用委托进行页面之间的传值,读取.plist文件,和使用代码创建视图,以及对tableview的操作.
一.第一个视图:
MyViewController
#import "MyViewController.h"
#import "MyCodeAndCountriesViewController.h"
@interface MyViewController (){
UILabel *leftLabel;
UILabel *rightLabel;
}
@end
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *titleView = [[UILabel alloc]initWithFrame:CGRectMake(60,7,200,30)];
[titleView setText:@"选择"];
titleView.textAlignment=UITextAlignmentCenter;
titleView.textColor=[UIColor blueColor];
self.navigationItem.titleView = titleView;
UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectBtn.frame = CGRectMake(15, 5, 290, 30);
leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 60, selectBtn.frame.size.height)];
leftLabel.textAlignment = UITextAlignmentCenter;
leftLabel.backgroundColor = [UIColor clearColor];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(75, 0, 1, selectBtn.frame.size.height)];
[imageView1 setBackgroundColor:[UIColor grayColor]];
rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(76, 0, 199, selectBtn.frame.size.height)];
rightLabel.textAlignment = UITextAlignmentCenter;
rightLabel.backgroundColor = [UIColor clearColor];
UIImageView *imageView2 = [[ UIImageView alloc ]initWithFrame:CGRectMake(270, 10, 10, 10)];
imageView2.image = [UIImage imageNamed:@"mini_blue_arrows.png"];
[selectBtn addSubview:leftLabel];
[selectBtn addSubview:imageView1];
[selectBtn addSubview:rightLabel];
[selectBtn addSubview:imageView2];
[selectBtn addTarget:self action:@selector(selectCodeAndCountry) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectBtn];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(void) selectCodeAndCountry{
MyCodeAndCountriesViewController *codeController = [[MyCodeAndCountriesViewController alloc] initWithNibName:@"MyCodeAndCountriesViewController" bundle:nil];
self.hidesBottomBarWhenPushed =YES;
codeController.delegate = self ;
[self.navigationController pushViewController:codeController animated:YES];
}
-(void) getCode:(NSString *)code AndCountry:(NSString *)country{
leftLabel.text = [@"+" stringByAppendingFormat:code];
rightLabel.text = country;
}
@end
2 委托. CodeAndCountryDelegate.h
#import <Foundation/Foundation.h>
@protocol CodeAndCountryDelegate <NSObject>
-(void)getCode:(NSString *)code AndCountry:(NSString *)country;
@end
3.选择国家代码和国家界面.
MyCodeAndCountriesViewController.h
#import <UIKit/UIKit.h>
#import "MyViewController.h"
@interface MyCodeAndCountriesViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
MyViewController *v;
}
@property (nonatomic ,strong) NSArray *countries;
@property (strong ,nonatomic) id<CodeAndCountryDelegate> delegate;
@end
MyCodeAndCountriesViewController.m
#import "MyCodeAndCountriesViewController.h"
#import "MyViewController.h"
@interface MyCodeAndCountriesViewController (){
NSArray *countryArray;
}
@end
@implementation MyCodeAndCountriesViewController
@synthesize delegate;
@synthesize countries;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.hidesBottomBarWhenPushed =YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *titleView = [[UILabel alloc]initWithFrame:CGRectMake(60,7,200,30)];
titleView.text =@"选择国家和地区";
titleView.textAlignment=UITextAlignmentCenter;
titleView.textColor=[UIColor blueColor];
self.navigationItem.titleView = titleView;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"CodeAndCountries" ofType:@"plist"];
NSMutableArray *arrry = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
self.countries = arrry;
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [countries count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
countryArray = [[countries objectAtIndex:section]objectForKey:@"Countries"];
return [countryArray count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString* headTitle = [[countries objectAtIndex:section]objectForKey:@"Head"];
return headTitle;
NSString *sectionTitle=[self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle==nil) {
return nil;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
static NSString *cellIndentifier = @"Cell";
countryArray = [[countries objectAtIndex:section]objectForKey:@"Countries"] ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier ];
if(cell ==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [[countryArray objectAtIndex:row]objectForKey:@"country"];
cell.detailTextLabel.text = [[countryArray objectAtIndex:row]objectForKey:@"code"];
return cell;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray *array = [NSMutableArray arrayWithCapacity:20];
for (NSDictionary *dict in countries) {
[array addObject:[dict objectForKey:@"Head"]];
}
return array;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
countryArray = [[countries objectAtIndex:section]objectForKey:@"Countries"] ;
NSString *codeStrings = [[countryArray objectAtIndex:row] objectForKey:@"code"];
NSString *countryStrings = [[countryArray objectAtIndex:row ]objectForKey:@"country"];
[self.delegate getCode:codeStrings AndCountry:countryStrings];
[self.navigationController popViewControllerAnimated:YES];
}
@end
附.plist文件.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Head</key>
<string>A</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>355</string>
<key>country</key>
<string>阿尔巴尼亚</string>
</dict>
<dict>
<key>code</key>
<string>213</string>
<key>country</key>
<string>阿尔及利亚</string>
</dict>
<dict>
<key>code</key>
<string>93</string>
<key>country</key>
<string>阿富汗</string>
</dict>
<dict>
<key>code</key>
<string>54</string>
<key>country</key>
<string>阿根廷</string>
</dict>
<dict>
<key>code</key>
<string>353</string>
<key>country</key>
<string>爱尔兰</string>
</dict>
<dict>
<key>code</key>
<string>20</string>
<key>country</key>
<string>埃及</string>
</dict>
<dict>
<key>code</key>
<string>251</string>
<key>country</key>
<string>埃塞俄比亚</string>
</dict>
<dict>
<key>code</key>
<string>372</string>
<key>country</key>
<string>爱沙尼亚</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>B</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>375</string>
<key>country</key>
<string>白俄罗斯</string>
</dict>
<dict>
<key>code</key>
<string>1441</string>
<key>country</key>
<string>百慕大</string>
</dict>
<dict>
<key>code</key>
<string>92</string>
<key>country</key>
<string>巴基斯坦</string>
</dict>
<dict>
<key>code</key>
<string>55</string>
<key>country</key>
<string>巴西</string>
</dict>
<dict>
<key>code</key>
<string>359</string>
<key>country</key>
<string>保加利亚</string>
</dict>
<dict>
<key>code</key>
<string>32</string>
<key>country</key>
<string>比利时</string>
</dict>
<dict>
<key>code</key>
<string>354</string>
<key>country</key>
<string>冰岛</string>
</dict>
<dict>
<key>code</key>
<string>48</string>
<key>country</key>
<string>波兰</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>C</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>240</string>
<key>country</key>
<string>赤道几内亚</string>
</dict>
<dict>
<key>code</key>
<string>850</string>
<key>country</key>
<string>朝鲜</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>D</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>45</string>
<key>country</key>
<string>丹麦</string>
</dict>
<dict>
<key>code</key>
<string>49</string>
<key>country</key>
<string>德国</string>
</dict>
<dict>
<key>code</key>
<string>670</string>
<key>country</key>
<string>东帝汶</string>
</dict>
<dict>
<key>code</key>
<string>228</string>
<key>country</key>
<string>多哥</string>
</dict>
<dict>
<key>code</key>
<string>1767</string>
<key>country</key>
<string>多米尼加</string>
</dict>
<dict>
<key>code</key>
<string>1809</string>
<key>country</key>
<string>多米尼加共和国</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>E</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>593</string>
<key>country</key>
<string>厄尔多瓜</string>
</dict>
<dict>
<key>code</key>
<string>291</string>
<key>country</key>
<string>厄立特里亚</string>
</dict>
<dict>
<key>code</key>
<string>7</string>
<key>country</key>
<string>俄罗斯</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>F</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>33</string>
<key>country</key>
<string>法国</string>
</dict>
<dict>
<key>code</key>
<string>39</string>
<key>country</key>
<string>梵蒂冈</string>
</dict>
<dict>
<key>code</key>
<string>679</string>
<key>country</key>
<string>菲律宾</string>
</dict>
<dict>
<key>code</key>
<string>358</string>
<key>country</key>
<string>芬兰</string>
</dict>
<dict>
<key>code</key>
<string>500</string>
<key>country</key>
<string>福克兰群岛</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>G</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>242</string>
<key>country</key>
<string>刚果</string>
</dict>
<dict>
<key>code</key>
<string>57</string>
<key>country</key>
<string>哥伦比亚</string>
</dict>
<dict>
<key>code</key>
<string>53</string>
<key>country</key>
<string>古巴</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>H</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>509</string>
<key>country</key>
<string>海地</string>
</dict>
<dict>
<key>code</key>
<string>82</string>
<key>country</key>
<string>韩国</string>
</dict>
<dict>
<key>code</key>
<string>31</string>
<key>country</key>
<string>荷兰</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>J</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>1</string>
<key>country</key>
<string>加拿大</string>
</dict>
<dict>
<key>code</key>
<string>855</string>
<key>country</key>
<string>柬埔寨</string>
</dict>
<dict>
<key>code</key>
<string>420</string>
<key>country</key>
<string>捷克共和国</string>
</dict>
<dict>
<key>code</key>
<string>224</string>
<key>country</key>
<string>几内亚</string>
</dict>
<dict>
<key>code</key>
<string>233</string>
<key>country</key>
<string>加纳</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>K</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>237</string>
<key>country</key>
<string>喀麦隆</string>
</dict>
<dict>
<key>code</key>
<string>385</string>
<key>country</key>
<string>克罗地亚</string>
</dict>
<dict>
<key>code</key>
<string>254</string>
<key>country</key>
<string>肯尼亚</string>
</dict>
<dict>
<key>code</key>
<string>965</string>
<key>country</key>
<string>科威特</string>
</dict>
<dict>
<key>code</key>
<string>225</string>
<key>country</key>
<string>科特迪瓦</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>M</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>960</string>
<key>country</key>
<string>马尔代夫</string>
</dict>
<dict>
<key>code</key>
<string>60</string>
<key>country</key>
<string>马来西亚</string>
</dict>
<dict>
<key>code</key>
<string>389</string>
<key>country</key>
<string>马其顿</string>
</dict>
<dict>
<key>code</key>
<string>95</string>
<key>country</key>
<string>缅甸</string>
</dict>
<dict>
<key>code</key>
<string>51</string>
<key>country</key>
<string>秘鲁</string>
</dict>
<dict>
<key>code</key>
<string>1</string>
<key>country</key>
<string>美国</string>
</dict>
<dict>
<key>code</key>
<string>976</string>
<key>country</key>
<string>蒙古</string>
</dict>
<dict>
<key>code</key>
<string>880</string>
<key>country</key>
<string>孟加拉国</string>
</dict>
<dict>
<key>code</key>
<string>52</string>
<key>country</key>
<string>墨西哥</string>
</dict>
<dict>
<key>code</key>
<string>44</string>
<key>country</key>
<string>曼岛</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>N</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>264</string>
<key>country</key>
<string>纳米比亚</string>
</dict>
<dict>
<key>code</key>
<string>27</string>
<key>country</key>
<string>南非</string>
</dict>
<dict>
<key>code</key>
<string>977</string>
<key>country</key>
<string>尼泊尔</string>
</dict>
<dict>
<key>code</key>
<string>234</string>
<key>country</key>
<string>尼日利亚</string>
</dict>
<dict>
<key>code</key>
<string>47</string>
<key>country</key>
<string>挪威</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>P</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>680</string>
<key>country</key>
<string>帕劳</string>
</dict>
<dict>
<key>code</key>
<string>351</string>
<key>country</key>
<string>葡萄牙</string>
</dict>
<dict>
<key>code</key>
<string>870</string>
<key>country</key>
<string>皮特凯恩群岛</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>R</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>81</string>
<key>country</key>
<string>日本</string>
</dict>
<dict>
<key>code</key>
<string>46</string>
<key>country</key>
<string>瑞典</string>
</dict>
<dict>
<key>code</key>
<string>41</string>
<key>country</key>
<string>瑞士</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>T</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>993</string>
<key>country</key>
<string>土库曼斯坦</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>W</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>502</string>
<key>country</key>
<string>危地马拉</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>Y</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>1876</string>
<key>country</key>
<string>牙买加</string>
</dict>
<dict>
<key>code</key>
<string>967</string>
<key>country</key>
<string>也门</string>
</dict>
<dict>
<key>code</key>
<string>374</string>
<key>country</key>
<string>亚美尼亚</string>
</dict>
</array>
</dict>
<dict>
<key>Head</key>
<string>Z</string>
<key>Countries</key>
<array>
<dict>
<key>code</key>
<string>260</string>
<key>country</key>
<string>赞比亚</string>
</dict>
<dict>
<key>code</key>
<string>235</string>
<key>country</key>
<string>乍得</string>
</dict>
<dict>
<key>code</key>
<string>350</string>
<key>country</key>
<string>直布罗陀</string>
</dict>
<dict>
<key>code</key>
<string>56</string>
<key>country</key>
<string>智利</string>
</dict>
<dict>
<key>code</key>
<string>236</string>
<key>country</key>
<string>中非共和国</string>
</dict>
<dict>
<key>code</key>
<string>86</string>
<key>country</key>
<string>中国</string>
</dict>
<dict>
<key>code</key>
<string>853</string>
<key>country</key>
<string>中国澳门香港特别行政区</string>
</dict>
<dict>
<key>code</key>
<string>852</string>
<key>country</key>
<string>中国香港特别行政区</string>
</dict>
</array>
</dict>
</array>
</plist>
最后,在AppDelegate, didFinishLaunchingWithOption中添加如下代码:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"barcode_title_bg"] forBarMetrics:UIBarMetricsDefault];
self.viewController = [[MyViewController alloc] init];
self.naviController = [[UINavigationController alloc]init];
[self.naviController pushViewController:self.viewController animated:YES];
self.window.rootViewController = self.naviController;
注意:使用委托时,关键步骤就是必须设置委托代理.
如代码中的 codeController.delegate = self ; ,之前一直搞不明白.也就是说,如A视图中要求获取B视图中的值.那A就要实现委托方法.在A跳转到B界面时,将B的delegate设置为A.,然后在B执行操作返回A时,使用delegate(附带A要获取的值)发送委托消息.这样就实现了页面间的值的传递.有不对之处,请提出来.谢谢.