溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

發(fā)布時(shí)間:2020-07-13 13:32:19 來(lái)源:網(wǎng)絡(luò) 閱讀:326 作者:蓬萊仙羽 欄目:移動(dòng)開(kāi)發(fā)

我們?cè)趇Phone開(kāi)發(fā)的過(guò)程中,估計(jì)UINavgationController是最最常用的控件之一吧,截下來(lái)我就用一個(gè)demo來(lái)舉例導(dǎo)航控制器的應(yīng)用。包含了tableview中增刪查改的功能。

導(dǎo)航控制器的應(yīng)用Demo

實(shí)現(xiàn)步驟:

1.創(chuàng)建一個(gè)Empty項(xiàng)目,命名為Navdemo。

2.創(chuàng)建一個(gè)根視圖控制器,繼承自UINavgationController,命名為FirstViewController。

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

FirstViewController.h:

#import <UIKit/UIKit.h> @interface FirstViewController : UITableViewController @property(nonatomic,retain) NSMutableArray *array; @end

FirstViewController.m:

#import "FirstViewController.h" #import "DXWDiscosureButtonViewController.h" #import "DXWCheckViewController.h" @interface FirstViewController ()  @end  @implementation FirstViewController static NSString *CellIdentifier = @"Cell"; - (id)initWithStyle:(UITableViewStyle)style {     self = [super initWithStyle:style];     if (self) {         self.array = @[[[DXWDiscosureButtonViewController alloc] initWithStyle:UITableViewStylePlain],[[DXWCheckViewController alloc] initWithStyle:UITableViewStylePlain]];         self.title = @"First";     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad];     //注冊(cè)     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }  #pragma mark - Table view data source  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation.     // Return the number of sections.     return 1; }  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation.     // Return the number of rows in the section.     return [self.array count]; }  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     //[tableView registerClass:[SecondViewController class] forCellReuseIdentifier:@"aa"];          //這個(gè)多一個(gè)最后的一個(gè)參數(shù),必須要有上面一行注冊(cè)過(guò)的才能這樣用,不然的話就去掉最后一個(gè)參數(shù)     //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     //沒(méi)有注冊(cè)需要?jiǎng)?chuàng)建 //    if (cell == nil) { //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //    }     int row = [indexPath row];     //當(dāng)有多個(gè)view沒(méi)有繼承SecondVIewController的時(shí)候,可以這樣寫     //[((UITableViewController *)self.array[row] valueForKey:@"Title")];          cell.textLabel.text = ((DXWDiscosureButtonViewController *)self.array[row]).title;     cell.imageView.image = ((DXWDiscosureButtonViewController *)self.array[row]).image;     //有右邊的>符號(hào)     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;     return cell; }  #pragma mark - Table view delegate  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //     DXWDiscosureButtonViewController *detailViewController = [[DXWDiscosureButtonViewController alloc] init];     //[detailViewController release];     int row = [indexPath row];      [self.navigationController pushViewController:self.array[row] animated:YES]; }  @end

3.修改Delegate,設(shè)置根視圖控制器

#import "DXWAppDelegate.h" #import "FirstViewController.h"  //導(dǎo)入根視圖控制器頭文件 @implementation DXWAppDelegate  - (void)dealloc {     [_window release];     [super dealloc]; }  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];     //創(chuàng)建首頁(yè)     FirstViewController *first = [[FirstViewController alloc] initWithStyle:UITableViewStylePlain];     //創(chuàng)建一個(gè)導(dǎo)航欄,其中的首頁(yè)是FirstViewController     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first];     //將導(dǎo)航欄作為根視圖控制器     self.window.rootViewController  = nav;          self.window.backgroundColor = [UIColor whiteColor];     [self.window makeKeyAndVisible];     return YES; } 

4.創(chuàng)建所有子視圖的一個(gè)基類控制器,命名為SecondViewController

SecondViewController.h:

#import <UIKit/UIKit.h>  @interface SecondViewController : UITableViewController @property(nonatomic,retain)UIImage *image; @end

SecondViewController.m:

#import "SecondViewController.h"  @interface SecondViewController ()  @end  @implementation SecondViewController static NSString *cellRequestIdentifier = @"CellReuseIdentifier"; - (id)initWithStyle:(UITableViewStyle)style {     self = [super initWithStyle:style];     if (self) {         //self.title = @"SecondView";     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad];     //注冊(cè)     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellRequestIdentifier]; }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }  #pragma mark - Table view data source  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation.     // Return the number of sections.     return 1; }  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation.     // Return the number of rows in the section.     return 1; }  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CellIdentifier = @"Cell";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];          return cell; } #pragma mark - Table view delegate  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    }  @end 

5.創(chuàng)建二級(jí)子視圖控制器,也就是上圖所示的水果選項(xiàng)卡的內(nèi)容控制器,命名為DXWDiscosureButtonController,繼承自SecondViewController。

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

DXWDiscosureButtonController.h:

#import "SecondViewController.h"  @interface DXWDiscosureButtonViewController : SecondViewController @property(nonatomic,retain) NSMutableArray *array; @end

DXWDiscosureButtonController.m:

#import "DXWDiscosureButtonViewController.h" #import "ThirdViewController.h" @interface DXWDiscosureButtonViewController ()  @end  @implementation DXWDiscosureButtonViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.image = [UIImage imageNamed:@"disclosureButtonControllerIcon"];         self.title = @"水果";     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad];     self.array = [[NSMutableArray alloc] initWithObjects:@"apple",@"orange",@"pitch",@"lenmon",@"balana", nil]; }  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.array count]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CellIdentifier = @"Cell";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     if (cell == nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];     }     int row = [indexPath row];     cell.textLabel.text = [self.array objectAtIndex:row]; //    cell.imageView.image = ((DXWDiscosureButtonViewController *)self.array[row]).image;     //有右邊的>符號(hào)     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;     return cell; }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }  -(void)dealloc {     [self.array release];     [super dealloc]; }   -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     int row = [indexPath row];     UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Title" message:[NSString stringWithFormat:@"你選擇了%@",self.array[row]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];     [a show];      }  //右邊的>符號(hào)響應(yīng)事件 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {     int row = [indexPath row]; //    UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Title" message:[NSString stringWithFormat:@"你選擇了%@",self.array[row]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; //    [a show];          ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];     thirdViewController.str = self.array[row];     [self.navigationController pushViewController:thirdViewController animated:YES];     [thirdViewController release]; }  @end

6.創(chuàng)建第三級(jí)子視圖控制器,也就是點(diǎn)擊了某個(gè)水果選項(xiàng)卡的最右邊的那個(gè)綠色按鈕,會(huì)跳轉(zhuǎn)到第三個(gè)視圖,并且顯示你所點(diǎn)擊的水果名字,命名為ThirdViewController,繼承自普通的ViewController,并且?guī)в衳ib文件

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

ThirdViewController.h:

#import <UIKit/UIKit.h>  @interface ThirdViewController : UIViewController @property (retain, nonatomic) IBOutlet UILabel *lblshow; @property (nonatomic,copy) NSString *str; @end

ThirdViewController.m:

#import "ThirdViewController.h"  @interface ThirdViewController ()  @end  @implementation ThirdViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.title = @"ThirdViewController";     }     return self; }   - (void)viewDidLoad {     [super viewDidLoad];     self.lblshow.text = self.str; }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }  - (void)dealloc {     [_lblshow release];     [super dealloc]; } @end

7.創(chuàng)建根視圖控制器的第二個(gè)選項(xiàng)卡的二級(jí)視圖控制器,也就是選擇的省市選項(xiàng)卡,命名為DXWCheckViewController,繼承自tableViewController,不帶xib文件

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

實(shí)現(xiàn)的效果是點(diǎn)擊某行,這行顯示mark

DXWCheckViewController.h:

#import "SecondViewController.h"  @interface DXWCheckViewController : SecondViewController @property(nonatomic,retain) NSMutableArray *array; @property(nonatomic,assign) int selectedValue; @end

DXWCheckViewController.m:

#import "DXWCheckViewController.h"  @interface DXWCheckViewController ()  @end  @implementation DXWCheckViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.image = [UIImage imageNamed:@"checkmarkControllerIcon.png"];         self.title = @"省市";     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad];     self.selectedValue = -1;     self.array = [[NSMutableArray alloc] initWithObjects:@"北京",@"上海",@"重慶",@"天津",@"江蘇",@"浙江",@"河北",@"湖南",@"河南",@"湖北", nil]; }  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.array count]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CellIdentifier = @"Cell";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     if (cell == nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];     }     int row = [indexPath row];     cell.textLabel.text = [self.array objectAtIndex:row];     //    cell.imageView.image = ((DXWDiscosureButtonViewController *)self.array[row]).image;     //有右邊的>符號(hào)     //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;     if (self.selectedValue == row) {         cell.accessoryType = UITableViewCellAccessoryCheckmark;     }     else     {         cell.accessoryType = UITableViewCellAccessoryNone;     }     return cell; }  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     int row = [indexPath row];     //方法一:簡(jiǎn)潔明了的方法     //self.selectedValue = row;     //[tableView reloadData];     //方法二:現(xiàn)貨的以前的選中的cell,將其accessoryType設(shè)為None     NSIndexPath *path = [NSIndexPath indexPathForRow:self.selectedValue inSection:0];     //將軒中行的accessoryType設(shè)為Checkmark     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:path];     oldCell.accessoryType = UITableViewCellAccessoryNone;     //將selectedValue設(shè)為當(dāng)前軒中行     self.selectedValue = row;     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];     cell.accessoryType = UITableViewCellAccessoryCheckmark; }  -(void)dealloc {     [self.array release];     [super dealloc]; }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];      }  @end

8.添加繼承自SecondViewController的班級(jí)選項(xiàng)卡的二級(jí)視圖控制器,命名為ButtonViewController

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

ButtonViewController.h:

#import "SecondViewController.h"  @interface ButtonViewController : SecondViewController @property(retain,nonatomic) NSArray *array; @end

ButtonViewController.m:

#import "ButtonViewController.h"  @interface ButtonViewController ()  @end  @implementation ButtonViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.image = [UIImage imageNamed:@"rowControlsIcon"];         self.title = @"班級(jí)";     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad]; 	self.array = [NSArray arrayWithObjects:@"一班",@"二班",@"三班",@"四班",@"五班",@"六班",@"七班",@"八班",@"九班",@"十班",@"十一班",@"十二班",@"十三班",@"十四班",nil]; } //行數(shù) -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.array count]; }   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *identifier = @"Identifier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];     if (cell == nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];         UIImage *image = [UIImage imageNamed:@"button_up"];//設(shè)置button的背景圖片         UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];         [button setBackgroundImage:image forState:UIControlStateNormal];                  //button.frame = CGRectMake(0, 0, 150, 30);         [button sizeToFit];//自適應(yīng)圖片的大小         [button setTitle:@"詳細(xì)信息" forState:UIControlStateNormal];         [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];         cell.accessoryView = button;     }     cell.accessoryView.tag = [indexPath row];     int row = [indexPath row];     cell.textLabel.text = self.array[row];     return cell; }  -(void)buttonClick:(id)sender {     //通過(guò)button的上一級(jí)可以拿到那個(gè)cell,在通過(guò)cell來(lái)拿到它所在的行數(shù),然后可以獲得相應(yīng)的數(shù)據(jù)     UIButton *button = sender;     UITableViewCell *cell = (UITableViewCell *)[button superview];     NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];     int row = [indexpath row];     NSLog(@"%d班",row+1);     UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"詳細(xì)信息" message:[NSString stringWithFormat:@"%d班",row+1] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];     [a show]; }  @end 


9.創(chuàng)建可以移動(dòng)的tableview效果,命名為MoveViewController,繼承自SecondViewController,不帶xib

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

操作:點(diǎn)擊Edit按鈕之后,可以點(diǎn)擊每行右邊的按鈕進(jìn)行拖動(dòng)。

MoveViewController.h:

#import "SecondViewController.h"  @interface MoveViewController : SecondViewController @property(retain,nonatomic)NSMutableArray *array; @end

MoveViewController.m:

#import "MoveViewController.h"  @interface MoveViewController ()  @end  @implementation MoveViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.title = @"Move";         self.image = [UIImage imageNamed:@"moveMeIcon"];         NSArray *arr = @[@"小明",@"小花",@"小李",@"小王",@"小丁",@"小張",@"小康",@"小劉",@"小墩",@"大墩"];         self.array = [arr mutableCopy];     }     return self; }  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.array count]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *identifier = @"Identifier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];     if (cell == nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];     }     int row = [indexPath row];     cell.textLabel.text = self.array[row];     return cell; }  - (void)viewDidLoad {     [super viewDidLoad];     //首先要添加右上角的一個(gè)edit按鈕,按鈕按下去可以設(shè)置可以編輯     UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)];     //[button setTitle:@"move"]; 	self.navigationItem.rightBarButtonItem = button; }  -(void)itemButtonClick:(id)sender {     //設(shè)置可以編輯     [self.tableView setEditing:!self.tableView.editing];//設(shè)置成和當(dāng)前狀態(tài)相反的狀態(tài) } //設(shè)置編譯圖標(biāo) -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleNone; }  //設(shè)置是否可以移動(dòng) -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {     return YES; }  -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {     id object= self.array`sourceIndexPath row`;     [self.array removeObject:self.array`sourceIndexPath row`];     [self.array insertObject:object atIndex:[destinationIndexPath row]];     NSLog(@"%d",[self.array count]); }  @end

10.創(chuàng)建一個(gè)帶刪除功能的tableViewController,同樣繼承自SecondViewController,不帶xib

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

DelViewController.h:

#import "SecondViewController.h"  @interface DelViewController : SecondViewController @property(retain,nonatomic)NSMutableArray *array; @end

DelViewController.m:

#import "DelViewController.h"  @interface DelViewController ()  @end  @implementation DelViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.title = @"Delete";         self.image = [UIImage imageNamed:@"deleteMeIcon"];         NSArray *arr = @[@"小明",@"小花",@"小李",@"小王",@"小丁",@"小張",@"小康",@"小劉",@"小墩",@"大墩"];         self.array = [arr mutableCopy];     }     return self; }  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.array count]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *identifier = @"Identifier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];     if (cell == nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];     }     int row = [indexPath row];     cell.textLabel.text = self.array[row];     return cell; }   - (void)viewDidLoad {     [super viewDidLoad]; 	//首先要添加右上角的一個(gè)edit按鈕,按鈕按下去可以設(shè)置可以編輯     UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)];     //[button setTitle:@"move"]; 	self.navigationItem.rightBarButtonItem = button; }   -(void)itemButtonClick:(id)sender {     //設(shè)置可以編輯     [self.tableView setEditing:!self.tableView.editing];//設(shè)置成和當(dāng)前狀態(tài)相反的狀態(tài)     if (self.tableView.editing) {         [self.navigationItem.rightBarButtonItem setTitle:@"Done"];     }     else     {         [self.navigationItem.rightBarButtonItem setTitle:@"Delete"];     } } //設(shè)置編譯圖標(biāo) -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleDelete; }  //將數(shù)據(jù)從數(shù)組和tableview中刪掉 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {     int row = [indexPath row];     [self.array removeObjectAtIndex:row];     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; } //可以根據(jù)行來(lái)設(shè)置delete按鈕位置上button的標(biāo)題 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {     return @"我要?jiǎng)h你"; }  @end

11.創(chuàng)建一個(gè)繼承自SecondViewController,具有添加功能的視圖控制器,命名為AddViewController

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

AddViewController.h:

#import "SecondViewController.h"  @interface AddViewController : SecondViewController<UIAlertViewDelegate> @property(retain,nonatomic)NSMutableArray *array; @property(retain,nonatomic)NSIndexPath *indexPath; @end

AddViewController.m:

#import "AddViewController.h"  @interface AddViewController ()  @end  @implementation AddViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.title = @"Add";         self.image = [UIImage imageNamed:@"deleteMeIcon"];         NSArray *arr = @[@"小明",@"小花",@"小李",@"小王",@"小丁",@"小張",@"小康",@"小劉",@"小墩",@"大墩"];         self.array = [arr mutableCopy];     }     return self; }  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.array count]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *identifier = @"Identifier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];     if (cell == nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];     }     int row = [indexPath row];     cell.textLabel.text = self.array[row];     return cell; }   - (void)viewDidLoad {     [super viewDidLoad]; 	//首先要添加右上角的一個(gè)edit按鈕,按鈕按下去可以設(shè)置可以編輯     UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(itemButtonClick:)];     //[button setTitle:@"move"]; 	self.navigationItem.rightBarButtonItem = button; }   -(void)itemButtonClick:(id)sender {     //設(shè)置可以編輯     [self.tableView setEditing:!self.tableView.editing];//設(shè)置成和當(dāng)前狀態(tài)相反的狀態(tài)     if (self.tableView.editing) {         [self.navigationItem.rightBarButtonItem setTitle:@"Done"];     }     else     {         [self.navigationItem.rightBarButtonItem setTitle:@"Delete"];     } } //設(shè)置編譯圖標(biāo) -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleInsert; }  //實(shí)現(xiàn)UIAlertView代理協(xié)議 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {     //獲取AlertView中的數(shù)據(jù)     NSString *str = [alertView textFieldAtIndex:0].text;     //將數(shù)據(jù)插入到array數(shù)組中(位置是點(diǎn)擊的位置的下一行)     [self.array insertObject:str atIndex:[self.indexPath row]+1];     //用點(diǎn)擊位置的下一行創(chuàng)建一個(gè)新的NSIndexPath     int newrow = [self.indexPath row]+1;     NSIndexPath *index = [NSIndexPath indexPathForRow:newrow inSection:0];     //在tableView的這個(gè)NSIndexPath中插入數(shù)據(jù)     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation: UITableViewRowAnimationFade];      }  //添加 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {     //獲得點(diǎn)擊的indexPath     self.indexPath = indexPath;     //創(chuàng)建一個(gè)UIAlertView,用來(lái)獲得數(shù)據(jù)     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"enterstring" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];     //帶有輸入框的UIAlertView     alert.alertViewStyle = UIAlertViewStylePlainTextInput;     [alert show];          //添加固定的內(nèi)容 //    [self.array insertObject:@"aaa" atIndex:[indexPath row]]; //    int row = [indexPath row]; //    [self.array removeObjectAtIndex:row]; //    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; }   @end 

12.創(chuàng)建一個(gè)Person類,具有名字和年齡屬性

Person.h:

#import <Foundation/Foundation.h>  @interface Person : NSObject<NSCopying> @property(copy,nonatomic) NSString *name; @property(assign,nonatomic) int age; @end

Person.m:

#import "Person.h"  @implementation Person  -(id)copyWithZone:(NSZone *)zone {     Person  *p = [[Person alloc] init] ;  //拷貝函數(shù)不需要release,這里用autorelease會(huì)報(bào)錯(cuò)     p.name = [self.name copy];     p.age = self.age;     return p; }  -(NSString *)description {     NSLog(@"%@,%d",self.name,self.age); }  @end



13.創(chuàng)建繼承自SecondViewcontroller的具有修改功能的tableViewController,命名為ChangeViewController,不帶xib文件:

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

ChangeViewController.h:

#import "SecondViewController.h" #import "Person.h"  //申明協(xié)議,修改自己頁(yè)面中的內(nèi)容 @protocol change <NSObject> //協(xié)議1 //-(void)changeData:(id)controller;//這里也可以是子視圖的controller對(duì)象,然后想獲取對(duì)象里面的任何值都可以 //協(xié)議2 -(void)changeData:(int)row Per:(Person *)p;  @end  @interface ChangeViewController : SecondViewController<change> @property(retain,nonatomic)NSMutableArray *array; @property(copy,nonatomic)Person *per; //-(void)changeData:(int)row; @end

ChangeViewController.m:

#import "ChangeViewController.h" #import "EditViewController.h" @interface ChangeViewController ()  @end  @implementation ChangeViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.title = @"修改";         self.image = [UIImage imageNamed:@"detailEditIcon"];         self.array = [[NSMutableArray alloc] initWithCapacity:5];         for (int i=0; i<5; i++) {             Person *p = [[Person alloc] init];             p.name = [NSString stringWithFormat:@"xiaoming%d",i];             p.age = 20+i;             [self.array addObject:p];         }                   }     return self; }  - (void)viewDidLoad {     [super viewDidLoad]; 	 }  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.array count]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     int row = [indexPath row];     static NSString *identifier = @"Identifier";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];     if (cell == nil) {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];     }     Person *p = self.array[row];     cell.textLabel.text = p.name;     cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",p.age];     return cell; }   //實(shí)現(xiàn)遵循協(xié)議的方法 -(void)changeData:(int)row Per:(Person *)p {     [self.array replaceObjectAtIndex:row withObject:p];     [self.tableView reloadData]; }   -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {          Person *p = self.array[indexPath.row];     NSLog(@"%@,%d",p.name,p.age);     EditViewController *edit = [[EditViewController alloc] init];     edit.per = [p copy];     edit.row = indexPath.row;     edit.delegate = self;     [self.navigationController  pushViewController:edit animated:YES]; }  @end


14.創(chuàng)建可以保存修改值的三級(jí)子試圖控制器,帶有xib

[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo[IOS]非常不錯(cuò)的導(dǎo)航控制器的應(yīng)用Demo

EditViewController.h:

#import <UIKit/UIKit.h> #import "Person.h" #import "ChangeViewController.h" @interface EditViewController : UIViewController @property(copy,nonatomic)Person *per;//對(duì)象 @property (retain, nonatomic) IBOutlet UITextField *name; @property (retain, nonatomic) IBOutlet UITextField *age; @property(assign,nonatomic) int row; //位置 @property(nonatomic,retain)id<change>delegate; @end

EditViewController.m:

#import "EditViewController.h"  @interface EditViewController ()  @end  @implementation EditViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         self.title = @"信息修改";     }     return self; }  -(void)viewWillAppear:(BOOL)animated {     self.name.text = self.per.name;     self.age.text = [NSString stringWithFormat:@"%d",self.per.age]; }  - (void)viewDidLoad {     [super viewDidLoad];     //首先要添加右上角的一個(gè)edit按鈕,按鈕按下去可以設(shè)置可以編輯     UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(itemLeftButtonClick:)]; 	self.navigationItem.leftBarButtonItem = button;          UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(itemRightButtonClick:)];     self.navigationItem.rightBarButtonItem = button1; } //返回 -(void)itemLeftButtonClick:(id)sender {     [self.navigationController popViewControllerAnimated:YES]; } //保存 -(void)itemRightButtonClick:(id)sender {     [self changedata];     [self.navigationController popViewControllerAnimated:YES]; }   - (void)changedata {     self.per.name = self.name.text;     self.per.age = [self.age.text intValue];     if ([self.delegate respondsToSelector:@selector(changeData:Per:)]) {         [self.delegate changeData:self.row Per:self.per];     } }  - (void)dealloc {     [_name release];     [_age release];     [super dealloc]; } @end


Demo源文件:

http://download.csdn.net/detail/s10141303/5999165


==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互學(xué)習(xí),共同進(jìn)步 ===================

轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/10170290

歡迎關(guān)注我的微博:http://weibo.com/u/2590571922
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI