UITableView传值(自己使用)(属性,代理传值)
2024-10-19 08:59:50
今天有些匆忙。
效果图如下:
代码如下:
#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
最新文章
- 云计算之路-阿里云上:“黑色1秒”最新线索——w3tp与w3dt
- loadRunner录制脚本常见问题及解决方法
- SQL SERVER 2008中输入汉字乱码的问题
- Activiti系列: 如何添加自定义表单引擎
- 【积硅计划】html标签
- 网络流(最大流) CodeForces 546E:Soldier and Traveling
- dp优化
- 浅谈API设计
- android Spinner 续
- aps.net验证控件的异常处理
- Selenium Grid2
- Settings.settings
- HTML编码和CSS编码会遇到的问
- 使用Js进行linq处理
- Hello jna
- 用keytool制作证书并在tomcat配置https服务(一)
- 第一册:lesson sixty one.
- 操作过程-CentOS7下添加新硬盘扩充已经存在的逻辑卷分区的存储空间
- 解决zabbix可用性为灰色状态
- 交互题(二分)(D. Game with modulo)