我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

[IOS]非常不错的导航控制器的应用Demo-创新互联

我们在iPhone开发的过程中,估计UINavgationController是最最常用的控件之一吧,截下来我就用一个demo来举例导航控制器的应用。包含了tableview中增删查改的功能。

成都创新互联是一家专注于成都做网站、成都网站设计、成都外贸网站建设与策划设计,新吴网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:新吴等地区。新吴做网站价格咨询:18982081108

导航控制器的应用Demo

实现步骤:

1.创建一个Empty项目,命名为Navdemo。

2.创建一个根视图控制器,继承自UINavgationController,命名为FirstViewController。

[IOS]非常不错的导航控制器的应用Demo

FirstViewController.h:

#import  @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];     //注册     [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"];          //这个多一个最后的一个参数,必须要有上面一行注册过的才能这样用,不然的话就去掉最后一个参数     //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     //没有注册需要创建 //    if (cell == nil) { //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //    }     int row = [indexPath row];     //当有多个view没有继承SecondVIewController的时候,可以这样写     //[((UITableViewController *)self.array[row] valueForKey:@"Title")];          cell.textLabel.text = ((DXWDiscosureButtonViewController *)self.array[row]).title;     cell.imageView.image = ((DXWDiscosureButtonViewController *)self.array[row]).image;     //有右边的>符号     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,设置根视图控制器

#import "DXWAppDelegate.h" #import "FirstViewController.h"  //导入根视图控制器头文件 @implementation DXWAppDelegate  - (void)dealloc {     [_window release];     [super dealloc]; }  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];     //创建首页     FirstViewController *first = [[FirstViewController alloc] initWithStyle:UITableViewStylePlain];     //创建一个导航栏,其中的首页是FirstViewController     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first];     //将导航栏作为根视图控制器     self.window.rootViewController  = nav;          self.window.backgroundColor = [UIColor whiteColor];     [self.window makeKeyAndVisible];     return YES; }

4.创建所有子视图的一个基类控制器,命名为SecondViewController

SecondViewController.h:

#import   @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];     //注册     [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.创建二级子视图控制器,也就是上图所示的水果选项卡的内容控制器,命名为DXWDiscosureButtonController,继承自SecondViewController。

[IOS]非常不错的导航控制器的应用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;     //有右边的>符号     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];      }  //右边的>符号响应事件 -(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.创建第三级子视图控制器,也就是点击了某个水果选项卡的最右边的那个绿色按钮,会跳转到第三个视图,并且显示你所点击的水果名字,命名为ThirdViewController,继承自普通的ViewController,并且带有xib文件

[IOS]非常不错的导航控制器的应用Demo

ThirdViewController.h:

#import   @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.创建根视图控制器的第二个选项卡的二级视图控制器,也就是选择的省市选项卡,命名为DXWCheckViewController,继承自tableViewController,不带xib文件

[IOS]非常不错的导航控制器的应用Demo

实现的效果是点击某行,这行显示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;     //有右边的>符号     //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];     //方法一:简洁明了的方法     //self.selectedValue = row;     //[tableView reloadData];     //方法二:现货的以前的选中的cell,将其accessoryType设为None     NSIndexPath *path = [NSIndexPath indexPathForRow:self.selectedValue inSection:0];     //将轩中行的accessoryType设为Checkmark     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:path];     oldCell.accessoryType = UITableViewCellAccessoryNone;     //将selectedValue设为当前轩中行     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的班级选项卡的二级视图控制器,命名为ButtonViewController

[IOS]非常不错的导航控制器的应用Demo

ButtonViewController.h:

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

ButtonViewController.m:

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:[IOS]非常不错的导航控制器的应用Demo-创新互联
文章起源:http://mswzjz.cn/article/dhjpid.html

其他资讯