iOS--页面跳转(UITableView)
本文只要实现运用(UITableView)表格实现页面的传值,同时运用了代理(委托)传值。
目录文件列表如下:

AddressBookViewController.h
#import <UIKit/UIKit.h>
#import "ContentViewController.h"
@interface AddressBookViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,postValueDelegate> @end
AddressBookViewController.m
#import "AddressBookViewController.h" @interface AddressBookViewController () @property(strong,nonatomic) UITableView *adderbookview;
@property(strong,nonatomic) NSMutableArray *person;
@property(strong,nonatomic) NSString *str; // 定义一个全局变量来接收行数
@property(assign,nonatomic)int number; @end @implementation AddressBookViewController - (void)viewDidLoad {
// 设置导航栏名称
self.view.backgroundColor=[UIColor colorWithRed:0.344 green:0.976 blue:1.000 alpha:1.000];
self.title=@"通讯录"; // 设置导航栏右边按钮
UIBarButtonItem *nextItem=[[UIBarButtonItem alloc] initWithTitle:@"next" style: UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
self.navigationItem.rightBarButtonItem=nextItem; self.person=[NSMutableArray array];
for (int i=; i<; i++) {
[self.person addObject:[NSString stringWithFormat:@"第%d个联系人",i]];
}
// 初始化 指定样式
self.adderbookview=[[UITableView alloc] initWithFrame:self.view.frame style:];
// 指定代理
self.adderbookview.delegate=self;
self.adderbookview.dataSource=self;
[self.view addSubview:self.adderbookview
]; [self.adderbookview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; self.adderbookview.separatorColor=[UIColor colorWithRed:1.000 green:0.739 blue:0.353 alpha:1.000]; }
#pragma mark - 跳转下一页的方法
-(void)nextPage
{
ContentViewController *contentV=[[ContentViewController alloc] init];
contentV.str=self.str;
[self.navigationController pushViewController:contentV animated:YES];
} #pragma mark - 代理方法 显示选中行的单元格信息
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",self.person[indexPath.row]); self.str=self.person[indexPath.row];
ContentViewController *contentV=[[ContentViewController alloc] init];
contentV.str=self.str;
contentV.delegate=self;
self.number=(int)indexPath.row; [self.navigationController pushViewController:contentV animated:YES];
} #pragma mark - 设置显示分区数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} #pragma mark - 数据源 每个分区对应的函数设置
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.person.count;
} #pragma mark - 数据源 每个单元格的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentity=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentity forIndexPath:indexPath];
cell.textLabel.text=self.person[indexPath.row]; return cell;
} #pragma mark - 实现代理的方法
-(void)postValue:(NSString *)str
{
[self.person replaceObjectAtIndex:self.number withObject:str];
[self.adderbookview reloadData];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
ContentViewController.h
#import <UIKit/UIKit.h> @protocol postValueDelegate <NSObject> -(void)postValue:(NSString *) str; @end @interface ContentViewController : UIViewController<UITextFieldDelegate> @property(strong,nonatomic) UITextField *textInfo;
@property(strong,nonatomic) NSString *str;
@property(strong,nonatomic) id<postValueDelegate> delegate; @end
ContentViewController.m
#import "ContentViewController.h"
@interface ContentViewController ()
@end
@implementation ContentViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航栏名称及整个背景的颜色
self.view.backgroundColor=[UIColor colorWithRed:1.000 green:0.955 blue:0.563 alpha:1.000];
self.title=@"详情";
// 设置导航栏左边的按钮
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"back" style: target:self action:@selector(backPage)];
// 添加输入框 UITextField
self.textInfo=[[UITextField alloc] initWithFrame:CGRectMake(, , , )];
self.textInfo.borderStyle=;
self.textInfo.text=self.str;
self.textInfo.delegate=self;
[self.view addSubview:self.textInfo];
}
#pragma mark - 返回上一页的方法
-(void)backPage
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 点击空白处隐藏键盘的方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.textInfo resignFirstResponder];
}
#pragma mark - 点击 return 返回的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
if (self.delegate) {
[self.delegate postValue:self.textInfo.text];
}
[self.navigationController popToRootViewControllerAnimated:YES];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
AppDelegate.h
#import <UIKit/UIKit.h>
#import "AddressBookViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AddressBookViewController *AddressBook=[[AddressBookViewController alloc] init];
UINavigationController *na=[[UINavigationController alloc] initWithRootViewController:AddressBook];
self.window.rootViewController=na;
return YES;
}
.........
@end
注意:
1、在第一页只需要点击UITableView对应的值;
2、在第二页需要按键盘上的 return 健才能传值到第一页。
运行结果:



2016-03-16
iOS--页面跳转(UITableView)的更多相关文章
- iOS页面跳转及数据传递
转: http://blog.csdn.net/wang9834664/article/details/8025571 iOS页面跳转: 第一种 [self.navigationController ...
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...
- ios页面跳转
reference:http://blog.csdn.net/engandend/article/details/11706323 目前,就我所学到的内容,页面跳转有三种方法 一.直接推到下一个页面 ...
- iOS 页面跳转,离开之前pop掉navigationController栈中的页面
http://blog.csdn.net/worldzhy/article/details/41312713 - (void)viewDidDisappear:(BOOL)animated { //因 ...
- ios基础之 透过页面跳转来认识 Strong 与 Weak
最近在自己做一个小程序,遇到了页面跳转的问题,然后上网一通乱搜,跳转的问题解决了,又有传值的问题.上面两个问题解决了,又发现内存比刚开始时多占用了2M,于是,各种内心纠结,想彻底 搞清楚strong ...
- iOS——使用StroryBoard页面跳转及传值
之前在网上搜iOS的页面跳转大多都是按回以前的那种xib的形式,但鄙人是使用storyboard的.这篇就只介绍利用storyboard进行页面跳转与传值. 新建页面 iOS的程序也是使用了MVC的思 ...
- phonegap + 推送 + 页面跳转 (ios)
os如果没有页面跳转的需求的话就使用极光推送,如果有页面跳转如果用了极光推送就只有用oc去写,但是我不会oc,所以智能放弃极光选用ios自己的apns来实现 新建项目跟安卓创建项目差不多,新建完成后就 ...
- ios&h5混合开发项目仿app页面跳转优化
前言:本人原本是ios开发工程师,但由于现今H5的兴起,行内刮起了一阵混合开发的风气,趁着这股劲,我也学了前端开发,不说研究的多深,但也能胜任日常的开发工作.长话短说,现今的混合开发应该还处于摸索阶段 ...
- iOS使用StroryBoard页面跳转及传值
之前在网上iOS的页面跳转大多都是按回以前的那种xib的形式,但鄙人是使用storyboard的.这篇就只介绍利用storyboard进行页面跳转与传值. 新建页面 iOS的程序也是使用了MVC的思想 ...
- IOS 页面间跳转
常用的就两种 一种通过导航,一种直接跳 第一种 直接跳转 思路大致就是new一个目的页面,然后设置下页面跳转动画 中间还可以做点目的页面的数据初始化: ValueInputView *valueVie ...
随机推荐
- ASP.NET Core开发-使用Nancy框架
Nancy简介 Nancy是一个轻量级的独立的框架,下面是官网的一些介绍: Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台,框架的目标是保持尽可能 ...
- ASP.NET MVC 学习之路由(URL Routing)
在ASP.NET MVC中,一个URL请求是由对应的一个Controller中的Action来处理的,由URL Routing来告诉MVC如何定位到正确的Controller和Action. 默认路由 ...
- SingalR--介绍
什么是SignalR? ASP.NET SignalR是为简化开发开发人员将实时web内容添加到应用程序过程而提供的类库.实时web功能指的是让服务器代码可以随时主动推送内容给客户端,而不是让服务器等 ...
- Web.config配置文件详解
整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <?xml v ...
- mybatis笔记2 基础理论准备
之前发了一篇mybatis的crud入门笔记,算是入门了,为了让功力加深一级,来研究下mybatis的理论知识,哈哈,以后好拿来跟技术经理吹吹牛- 按照问题来吧!个人觉得有自主意识,带着自己的问题来研 ...
- 线程Thread的基础知识学习
一.线程的基本概念 1.线程是一个程序内部的顺序控制流. 2.Java的线程是通过java.lang.Thread类来实现的. 3.VM启动时会有一个由主方法{public static void m ...
- [moka同学笔记]三、Yii2.0课程笔记(魏曦老师教程)关联字段增加搜索
关联字段增加搜索 post表关联adminuser表,通过post.author_id adminuser.id关联,在YII2.0生成搜索,关联字段搜索时,需要输入关联字段author的id才能搜 ...
- 设置placeholder字体颜色
/*设置placeholder字体颜色*/::-webkit-input-placeholder{ color: #FFF;}:-ms-input-placeholder{ color: #FFF;} ...
- 使用ADO.NET执行SQL脚本
public void ExecuteSql(SqlConnection connection, string sqlFile) { string sql = ""; using ...
- IClient for js开发之地图的加载
进行web开发之前首先需要安装IServer以及iClient for JavaScript的开发包.在这两中都具备的前提下进行第一步,如何调用IServer中发布的服务 调用iServer 中发布的 ...