今天有些匆忙。
效果图如下:
代码如下:
#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