欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS实现选择城市后跳转Tabbar效果

程序员文章站 2023-12-10 13:31:46
本文实例为大家分享了ios选择城市后跳转tabbar的具体实现代码,供大家参考,具体内容如下 一、效果图 二、工程图 三、代码 choosecityvi...

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

一、效果图

IOS实现选择城市后跳转Tabbar效果

IOS实现选择城市后跳转Tabbar效果

二、工程图

IOS实现选择城市后跳转Tabbar效果

三、代码

choosecityviewcontroller.h

#import <uikit/uikit.h>

@interface choosecityviewcontroller : uiviewcontroller
<uitableviewdelegate,uitableviewdatasource>
{
 nsmutablearray * dataarray;
 uitableview * mtableview;
}

@end 

 choosecityviewcontroller.m

#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.
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。