本文实例为大家分享了ios选择城市后跳转tabbar的具体实现代码,供大家参考,具体内容如下
一、效果图


二、工程图

三、代码
choosecityviewcontroller.h
?| 1 2 3 4 5 6 7 8 9 10 |
#import <uikit/uikit.h>
@interface choosecityviewcontroller : uiviewcontroller
<uitableviewdelegate,uitableviewdatasource>
{
nsmutablearray * dataarray;
uitableview * mtableview;
}
@end
|
choosecityviewcontroller.m
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#import "choosecityviewcontroller.h"
#import "detailviewcontroller.h"
@interface choosecityviewcontroller ()
@end
@implementation choosecityviewcontroller
- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil
{
self = [super initwithnibname:nibnameornil bundle:nibbundleornil];
if (self) {
// custom initialization
}
return self;
}
- (void)viewdidload
{
[super viewdidload];
// do any additional setup after loading the view.
//读取plist文件
[self readplistfile];
//初始化tableview
[self inittableview];
}
#pragma -mark -functions
-(void)readplistfile
{
dataarray = [[nsmutablearray alloc] initwithcapacity:0];
nsstring * path = [[nsbundle mainbundle] pathforresource:@"city" oftype:@"plist"];
nsdictionary * dict = [[nsdictionary alloc] initwithcontentsoffile:path];
nsenumerator * enumerator = [dict keyenumerator];
nsstring * key;
while (key = [enumerator nextobject]) {
nsdictionary * t = [dict objectforkey:key];
[dataarray addobject:t];
}
nslog(@"%@",dataarray);
}
-(void)inittableview
{
mtableview = [[uitableview alloc] initwithframe:self.view.bounds style:uitableviewstyleplain];
mtableview.delegate = self;
mtableview.datasource = self;
mtableview.autoresizingmask = uiviewautoresizingflexibleheight;
[self.view addsubview:mtableview];
}
#pragma -uitableviewdelegate
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
return [dataarray count];
}
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
static nsstring * id = @"cellid";
uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier:id];
if (cell == nil)
{
cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id];
}
nsdictionary *dict = [dataarray objectatindex:indexpath.row];
cell.textlabel.text = [dict objectforkey:@"city_name"];
return cell;
}
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath
{
nsdictionary * dict = [dataarray objectatindex:indexpath.row];
//把所选择的城市保存到本地
[[nsuserdefaults standarduserdefaults] setobject:[dict objectforkey:@"city_id"] forkey:@"city_id"];
[[nsuserdefaults standarduserdefaults] setobject:[dict objectforkey:@"city_name"] forkey:@"city_name"];
//跳转到另一个有tabbar的页面
detailviewcontroller *detail=[[detailviewcontroller alloc]init];
[self.navigationcontroller pushviewcontroller:detail animated:no];
}
- (void)didreceivememorywarning
{
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。