关于导航控制器+展示按钮,程序功能很简单写起来其实也很简单
这个程序一共分为三层,前两层为TableView视图,没有其他控件,所以创建时直接继承UITableViewController,那么我们就会的到一个TableView视图,今天这个程序在第一个视图中只提供一个功能,但是我们可以为它添加更多的功能,如果有很多功能,那么我们就可以为第二级视图创建一个统一的试图控制器.
所以,构建一个Windows-Base Application的程序,并穿件第一级视图控制器MyBaseViewController(.h,.m),并为导航控制器做必要的工作,{如果这里不清楚可以参考{<<iOS
Dev 初识 导航控制器>(http://blog.csdn.net/wintelsui/article/details/7447077)}
建立一个用于统一管理的继承与UITableViewController视图控制器MySecondViewController(.h,.m),
建立第二级别视图控制器FirstShowMoreViewController(.h,.m)并继承与MySecondViewController,
建立第三级视图控制器FirstShowMoreDetailViewController(.h,.m,.xib),
因为在第一级别视图中每一行都有一个图标,所以在MySecondViewController中要为每一个二级视图,添加一个图片供一级视图调用
MySecondViewController.h:
#import <UIKit/UIKit.h>
@interface MySecondViewController : UITableViewController
{
UIImage *ImageForRow;
}
@property (strong ,nonatomic) UIImage *ImageForRow;
@end
MySecondViewController.m:
#import "MySecondViewController.h"
#import "FirstShowMoreViewController.h"
@implementation MySecondViewController
@synthesize ImageForRow;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
@end
在这里我们删除了创建的时候自动生成的一些代码,因为这个只是供管理第二级别视图,但是地二级视图的显示由他们自己决定
第二级视图
MySecondViewController.h
#import "MySecondViewController.h"
#import "FirstShowMoreDetailViewController.h"
@interface FirstShowMoreViewController :MySecondViewController
{
NSArray *arraylist;
FirstShowMoreDetailViewController *firstShowMoreDetailViewController;
}
@property (retain) NSArray *arraylist;
@property (strong,nonatomic)FirstShowMoreDetailViewController *firstShowMoreDetailViewController;
@end
MySecondViewController.m
#import "FirstShowMoreViewController.h"
#import "MySecondViewController.h"
#import "MyOneAppDelegate.h"
@implementation FirstShowMoreViewController
@synthesize arraylist;
@synthesize firstShowMoreDetailViewController;
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
NSArray *arrayforlist = [[NSArrayalloc] initWithObjects:@"DongStone",@"Holydancer",@"Wintelsui",@"DongStone1",@"Holydancer1",@"Wintelsui1",@"DongStone2",@"Holydancer2",@"Wintelsui2",@"DongStone3",@"Holydancer3",@"Wintelsui3",nil];
self.arraylist = arrayforlist;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arraylistcount];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *pathindex =@"pathindex";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pathindex];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:pathindex];
}
cell.textLabel.text = [arraylistobjectAtIndex:[indexPath row]];
//设置右侧拓展按键为蓝色圆箭头
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *tempstring = [@"You have selected"stringByAppendingString:[self.arraylistobjectAtIndex:[indexPath row]]];
UIAlertView *alertview = [[UIAlertViewalloc]
initWithTitle:@"simpleness"
message:tempstring
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil ,nil];
[alertview show];
}
//这是一个新方法,用于处理右侧拓展图标的动作
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
if (self.firstShowMoreDetailViewController ==nil) {
self.firstShowMoreDetailViewController = [[FirstShowMoreDetailViewControlleralloc] init];
}
//这是第三级别视图显示内容,在第三级别视图中设置了一个message变量
firstShowMoreDetailViewController.message = [self.arraylistobjectAtIndex:[indexPath row]];
[self.navigationControllerpushViewController:firstShowMoreDetailViewControlleranimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
那么跳转到第三级视图,并使message的到我们传的值,在一个UILabel上显示出来,在第三级视图中,我们需要注意的就是,这次我们不是在ViewDidLoad中初始化,而是在
-(void)viewWillAppear:(BOOL)animated方法中初始化,这个方法区别在于ViewDidLoad只在视图自一次加载时调用,而
viewWillAppear:(BOOL)animated在每次加载页面的时候都会调用;
-(void)viewWillAppear:(BOOL)animated{
thislabel.text =message;
[super viewWillAppear:animated];
}
在看一下第一界别视图
MyBaseViewController.h:
#import <UIKit/UIKit.h>
#import "MySecondViewController.h"
#import "FirstShowMoreViewController.h"
@interface MyBaseViewController : UITableViewController
{
//用于装第二级视图控制器
NSArray *controllers;
}
@property (strong,nonatomic)NSArray *controllers;
@end
MyBaseViewController.m:
#import "MyBaseViewController.h"
@implementation MyBaseViewController
@synthesize controllers;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
//初始化第一个试图的内容
self.title =@"FirstList";
UIBarButtonItem *backbutton = [[UIBarButtonItemalloc] initWithTitle:@"HOME"style:UIBarButtonItemStyleBorderedtarget:nilaction:nil];
self.navigationItem.backBarButtonItem = backbutton;
NSMutableArray *arrayforcontrollers = [[NSMutableArrayalloc] init];
//添加FirstShowMore内容
FirstShowMoreViewController *firstshowmoreviewcontroller = [[FirstShowMoreViewControlleralloc] initWithStyle:UITableViewStylePlain];
firstshowmoreviewcontroller.title = @"ShowMore";
firstshowmoreviewcontroller.ImageForRow = [UIImageimageNamed:@"disclosureButtonControllerIcon.png"];
[arrayforcontrollers addObject:firstshowmoreviewcontroller];
//在这里我们数组内装的是对象的实例
self.controllers = arrayforcontrollers;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.controllerscount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
MySecondViewController *controller = [self.controllersobjectAtIndex:row];
cell.textLabel.text = controller.title;
cell.imageView.image = controller.ImageForRow;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//这样我们就把分配下一级视图的事儿交给了MySecondViewController
MySecondViewController *detailViewController = [self.controllersobjectAtIndex:[indexPath row]];
[self.navigationControllerpushViewController:detailViewController animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
分享到:
相关推荐
IOS父子控制器滑动导航
iOS中present出一个透明导航控制器NavigationController 的方法+毛玻璃效果
深入浅出iPhone/iPad开发(第2版)》是HeadFirst系列图书的一本最新力作。全书通过轻松的图文方式,由浅入深地向读者介绍SDK和Object-C的基础知识,以及如何构建并测试简单的应用模型,并提供如何利用iPhone/iPad的...
IOS应用源码——Navigation(导航控制器).zip
自定义导航栏,苹果原生的需要在控制器view的最左边才能右滑返回,因现在大多数App都需要全屏右滑返回到上一级controller、此demo可以实现,大致实现思路:在导航控制器view上添加拖拽手势,注释里面都有写!...
iOS Soft Dev Pack
iOS学习——block深入浅出博文中的代码 博文地址:http://blog.csdn.net/u010127917/article/details/49946527
这是我在学习ios的过程中写的一个demo,使用代码的方式创建导航控制器,并使用导航控制器管理其他的控制器。
创建好自己的控制器即可使用 地址https://github.com/leehyoley/ViewSelector.git
ios 新浪微博 主框架的实现 主控制器的封装 子控制器导航栏的统一样式 之后会一直更新上传
转自:http://code.cocoachina.com/detail/175890/iOS+7%E7%BC%A9%E6%94%BE%E5%AF%BC%E8%88%AA%E6%8E%A7%E5%88%B6%E5%99%A8/ iOS 7风格的缩放导航控制器 测试环境:Xcode 5.0,iOS 5.0以上
ios 控制器切换动画
简单轻便的分页控制器,改进上一个不支持实时滑动的效果,以及一些其他地方的改进。Github地址:https://github.com/CranzCapatain/iOSUtility
多控制器和导航控制器简单介绍 一、多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单。当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者...
ios源码之导航控制器Demo.rar
一个适用于iOS有趣,易用的标签栏导航控制器
3.3_3.7_iOS导航控制器
OS6开发指南视频-源码ppt:视图控制器与导航模式 通过本章的学习你已经可以判断你的应用是不是需要一个导航功能,并且知道在什么情况下选择平铺页面导航、标签导航、树形结构导航中的哪一种或者同时综合使用三种导航...