星空网 > 软件开发 > 操作系统

UITableView传值(自己使用)(属性,代理传值)

今天有些匆忙。

效果图如下:

 

 

UITableView传值(自己使用)(属性,代理传值)images/loading.gif' data-original="http://images2015.cnblogs.com/blog/868060/201603/868060-20160316193747709-937955542.png" />

UITableView传值(自己使用)(属性,代理传值)

 

UITableView传值(自己使用)(属性,代理传值)

 

UITableView传值(自己使用)(属性,代理传值)

 

 

 

代码如下:

#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传值(自己使用)(属性,代理传值)

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海外仓网址:https://www.goluckyvip.com/tag/33179.html
增值税申请:https://www.goluckyvip.com/tag/3318.html
海外仓尾程:https://www.goluckyvip.com/tag/33180.html
海外仓尾货:https://www.goluckyvip.com/tag/33181.html
海外仓问题:https://www.goluckyvip.com/tag/33182.html
海外仓无货源:https://www.goluckyvip.com/tag/33183.html
大连有啥好玩的景点-大连有啥好玩的景点推荐孩子:https://www.vstour.cn/a/411235.html
咸阳机场到临潼景点 西安咸阳机场到临潼有多远:https://www.vstour.cn/a/411236.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流