本文只要實現運用(UITableView)表格實現頁面的傳值,同時運用了代理(委托)傳值。 目錄文件列表如下: AddressBookViewController.h AddressBookViewController.m ContentViewController.h ContentViewCon
本文只要實現運用(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=1; i<15; i++) { [self.person addObject:[NSString stringWithFormat:@"第%d個聯繫人",i]]; } // 初始化 指定樣式 self.adderbookview=[[UITableView alloc] initWithFrame:self.view.frame style:1]; // 指定代理 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 1; } #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:2 target:self action:@selector(backPage)]; // 添加輸入框 UITextField self.textInfo=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)]; self.textInfo.borderStyle=2; 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