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 ...
随机推荐
- 【Android】Anroid5.0+新控件---酷炫标题栏的简单学习
Android5.0+推出的新控件感觉特别酷,最近想模仿大神做个看图App出来,所以先把这些新控件用熟悉了. 新控件的介绍.使用等等网上相应的文章已经特别多了,题主也没那能力去写篇详解出来,本篇随笔记 ...
- Android SDK Manager国内更新代理
在Android SDK Manager Setting 窗口设置HTTP Proxy server和HTTP Proxy Port这个2个参数,分别设置为: HTTP Proxy server:mi ...
- 【转载】css3 content 生成内容
content一般和:before,:after一起使用,用来生成内容(img和input没有该属性),content的内容一般可以为以下四种: none: 不生成任何值. attr: 插入标签属性值 ...
- C语言学习010:fopen读写文件
在文件input.csv文件中,我们有数据如下 Apple Pear Litchis Pineapple Watermelon 现在我们将input.csv文件下的读取并写入到output.csv文件 ...
- mysql-databaseython 3.4.0 with MySQL database
Phttp://shttp://stackoverflow.com/questions/23376103/python-3-4-0-with-mysql-databasetackoverflow.co ...
- Oracle修改默认字符编码
--查看Oracle数据库字符集: Sql代码 select userenv('language') from dual; 查询结果: SIMPLIFIED CHINESE_CHINA.AL32UTF ...
- c语言是如何实现泛型链表
最近有看一点Linux内核源码,发现内核里大量使用了list_head结构体.百度查了一下,原来内核利用这个结构体实现了泛型. 自认为对链表已经很熟悉的我,决定自己实现一下. 下面以Node和list ...
- Scalaz(0) - 写在前面
面向对象编程范畴(OOP)从80年代C++到90年代java的兴起已经经历了几十年的高潮,是不是已经发展到了尽头,该是函数式编程(FP)开始兴旺发达的时候了吧.这样说似乎心眼儿有点坏,可能会得罪当今大 ...
- 为什么正常安装并成功运行了Genymotion虚拟但是运行的时候启动的却是自带的模拟器?
最近因为这个问题困惑了好久,最终找到了解决思路: 点击genymotion——setting——ADB——Use sustom Android tools,找到电脑中SDK文件位置就可了! 希望自己坚 ...
- 初识Aop和扩展Aop
一.什么叫做AOp 解析:Aop(Aspect Oriented Programming)是面向切面编程,软件编程的一种思想. OOp(Object Oriented Programming)是面向对 ...