本文只要实现运用(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