IOS -- page Jump (UITableView), ios -- uitableview

Source: Internet
Author: User

IOS -- page Jump (UITableView), ios -- uitableview

In this article, you only need to use a (UITableView) table to implement page value transfer, and use a proxy (delegate) to pass the value.

The directory file list is as follows:

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; // define a global variable to receive the number of rows @ property (assign, nonatomic) int number; @ end @ implementation AddressBookViewController-(void) viewDidLoad {// set the navigation bar name self. view. backgroundColor = [UIColor colorWithRed: 0.344 green: 0.976 blue: 1.000 alpha: 1.000]; self. title = @ "Address Book"; // set 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 contacts", I];} // initialize the specified style self. adderbookview = [[UITableView alloc] initWithFrame: self. view. frame style: 1]; // specifies the proxy 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-Method for redirecting to the next page-(void) nextPage {ContentViewController * contentV = [[ContentViewController alloc] init]; contentV. str = self. str; [self. navigationController pushViewController: contentV animated: YES];} # The pragma mark-proxy method displays the cell information of the selected row-(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-set to display the number of partitions-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1 ;} # pragma mark-function settings for each partition of the data source-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. person. count ;}# pragma mark-content of each cell of the data source-(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-Method for proxy-(void) postValue :( NSString *) str {[self. person replaceObjectAtIndex: self. number withObject: str]; [self. adderbookview reloadData];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // 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]; // set the navigation bar name and the color self of the entire background. view. backgroundColor = [UIColor colorWithRed: 1.000 green: 0.955 blue: 0.563 alpha: 1.000]; self. title = @ "details"; // set the button self on the left of the navigation bar. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "back" style: 2 target: self action: @ selector (backPage)]; // Add the input box 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-Method for returning the previous page-(void) backPage {[self. navigationController popToRootViewControllerAnimated: YES];} # pragma mark-how to hide the keyboard in a blank space-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {[self. textInfo Response] ;}# pragma mark-click the return method-(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) didreceivemorywarning {[super didreceivemorywarning]; // 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

Note:

1. Click the value of UITableView on the first page;

2. on the second page, you must press the return key on the keyboard to transfer the value to the first page.

Running result:

2016-03-

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.