iOS--頁面跳轉(UITableView),ios--uitableview

來源:互聯網
上載者:User

iOS--頁面跳轉(UITableView),ios--uitableview

本文只要實現運用(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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.