今天有些匆忙。

效果图如下:

代码如下:

#import <UIKit/UIKit.h>
#import "FirstViewController.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *first=[[FirstViewController alloc]init];
UINavigationController *navc=[[UINavigationController alloc]initWithRootViewController:first];
self.window.rootViewController=navc;
return YES;
}
#import <UIKit/UIKit.h>
#import "SecondViewController.h" @interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,postValueDelegate> @property(strong,nonatomic)UITableView * tableview; @property(strong,nonatomic)NSMutableArray *array; @property(strong,nonatomic)NSString *st; @end
#import "FirstViewController.h"

@interface FirstViewController ()

@property(assign,nonatomic)int a;

@end

@implementation FirstViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
self.title=@"首页";
UIBarButtonItem *nextItem=[[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextpage)];
self.navigationItem.rightBarButtonItem=nextItem; self.array=[NSMutableArray array]; for (int i=1; i<20; i++) {
[self.array addObject:[NSString stringWithFormat:@"你摔了%d跤",i]];
} self.tableview=[[UITableView alloc]initWithFrame:self.view.frame style:1]; self.tableview.separatorColor=[UIColor redColor]; self.tableview.delegate=self; self.tableview.dataSource=self; [self.view addSubview:self.tableview]; [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
} -(void)postvalue:(NSString *)str
{
// self.st=str; [self.array replaceObjectAtIndex:self.a withObject:str]; [self.tableview reloadData]; // NSLog(@"%@",self.st); } #pragma mark 数据源 每个分区显示行数设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.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.array[indexPath.row]; return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ NSLog(@"%@",self.array[indexPath.row]); self.st=self.array[indexPath.row];
SecondViewController *second=[[SecondViewController alloc]init];
second.str=self.st;
second.delegate=self;
self.a=(int)indexPath.row; NSLog(@"%d",self.a); [self.navigationController pushViewController:second animated:YES];
} -(void)nextpage
{
SecondViewController *second=[[SecondViewController alloc]init]; second.str=self.st; [self.navigationController pushViewController:second animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
#import <UIKit/UIKit.h>
//创建协议,声明方法
@protocol postValueDelegate <NSObject> -(void)postvalue:(NSString* )str; @end @interface SecondViewController : UIViewController<UITextFieldDelegate> @property(strong,nonatomic) UITextField * textName; @property(strong,nonatomic) NSString *str; @property(strong,nonatomic) id<postValueDelegate> delegate; @end
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor greenColor];
self.navigationItem.hidesBackButton=YES;
self.title=@"尾页";
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"back" style:2 target:self action:@selector(backpage)]; self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 300, 150, 60)]; self.textName.borderStyle=1; self.textName.text=self.str; self.textName.delegate=self; [self.view addSubview:self.textName];
} -(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
} if (self.delegate) {
[self.delegate postvalue:self.textName.text]; } [self.navigationController popToRootViewControllerAnimated:YES]; return YES;
} -(void)backpage
{
if (self.delegate) {
[self.delegate postvalue:self.textName.text];
NSLog(@"%@",self.textName.text);
} [self.navigationController popToRootViewControllerAnimated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

UITableView传值(自己使用)(属性,代理传值)的更多相关文章

  1. iOS 页面间几种传值方式(属性,代理,block,单例,通知)

    第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...

  2. iOS传值方式:属性,代理,block,单例,通知

    正向传值均可,反向传值除属性传值不可,其余均可.下面简单介绍: (一)属性传值 第二个界面中的lable显示第一个界面textField中的文本 首先我们建立一个RootViewControllers ...

  3. 四大传值详解:属性传值,单例传值,代理传值,block传值

    一:属性传值 传值情景:从前一个页面向后一个页面传值 a.在后一个页面,根据传值类型和个数,写属性 b.在前一个页面, 为属性赋值 c.在后一个页面, 使用值 例如: 第一个视图: #import & ...

  4. iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值

    有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...

  5. iOS--页面间的代理传值(属性、代理(委托)、代码块、单例、通知)

    (一)属性传值 (二)代理(委托)传值 代理传值 适用于 反向传值 (从后往前传) 1.1 创建协议 及协议方法 在反向传值的页面(SecondViewController)中 1.2 创建协议类型的 ...

  6. iOS 页面间传值 之 属性传值,代理传值

    手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...

  7. Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值

    一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...

  8. OS笔记047代理传值和block传值

    在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...

  9. View 与 Controller 之间的delegate(代理)传值

    这个代理传值是经常使用的一种传值方式,下面介绍一种View 和 Controller 之间的代理传值方法. 先建立一个View视图 如 LoginView 是继承于一个UIView 在LoginVie ...

  10. IOS pop使用代理传值

    假如oneViewController页面push到OtherViewController页面,然后你想从OtherViewController页面pop到oneViewController页面的时候 ...

随机推荐

  1. The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 错误的原因及解决方案

    异常描述: 数据访问用EF,在数据库中用getdate()设置的默认值,程序中没有赋值. 出现异常. 此错误在百度上在我写此文之前没有多少解决方案,谷歌之等到以下两个有用的页: http://stac ...

  2. Java中不同转换符实现不同数据类型到字符串的转换

    String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重载形式. form ...

  3. 类库,委托,as.is,var,泛型集合

    类库: 就是让别人调用你写的方法,并且不让别人看到你是怎么实现的.(比如说一些核心文件) 如果有功能你不会做,需要别人帮忙,那么你的同事可以帮你写好一个类,然后你来调用这个类中的方法,完成你的项目. ...

  4. 【转载】[jquery.validate]自定义方法实现"手机号码或者固定电话"的逻辑验证

    最近项目开发中遇到这样的需求“手机号码或者固话至少填写一个”,如下图所示: 项目采用的jquery.validate.js验证组件,目前组件不支持这种“或”逻辑的验证,于是就自己定义一个 jQuery ...

  5. 观察者模式(Observer pattern)

    知识点 使对象之间达到松耦合的效果. 观察者模式定义了对象之间一对多的关系.主题用一个共同的接口来更新观察者. 观察者和被观察者之间通过松耦合的方式结合,被观察者不用理会观察者的实现细节,只需要观察者 ...

  6. 瀑布式开发、迭代开发、敏捷开发、XP与SCRUM的区别

    瀑布式开发.迭代开发,区别[都属于,生命周期模型]         两者都是一种开发模式,就像设计模式一样,考虑的角度不一样,个人感觉谈不到取代一说.         传统的瀑布式开发,也就是从需求到 ...

  7. The SQL Server Service Broker for the current database is not enabled, and as a result query notifications are not supported.

    当Insus.NET尝试解决此问题<When using SqlDependency without providing an options value, SqlDependency.Star ...

  8. PHP 判断是否为 AJAX 请求

    先说前端使用 jQuery 时怎么区分: jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest 在后端可以 ...

  9. Ajax学习笔记1之第一个Ajax应用程序

    代码 <head> <title>An Ajax demo</title> <script src="../js/jquery-1.4.1.js&q ...

  10. 驱动开发利器Microsoft Windows Driver Kit 7.1.0下载

    在Windows 2000 与Windows XP 系统采用是WINDDK来开发WINDOWS驱动程序,我手头也有WINDDK,可是从Windows Vista开始之后,一般采用Microsoft W ...