委托下是一个UINavigationController导航控制器。三个视图的先后切换,分别为UITableViewController、UITableViewController、UIViewController。
导航控制器的
pushViewController:animated:
Pushes a view controller onto the receiver’s stack and updates the display.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
是push视图控制器
1、创建一个空模板,在委托中建立一个导航控制器。
在AppDelegate.h中添加:
@property (strong, nonatomic) UINavigationController *myNaviController;
AppDelegate.m文件修改如下:
#import "AppDelegate.h"
#import "RootViewController.h"
@synthesize myNaviController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
RootViewController *rootView = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
self.myNaviController = [[UINavigationController alloc] initWithRootViewController:rootView];
[self.window addSubview:self.myNaviController.view];
[self.window setRootViewController:self.myNaviController];
self.window.backgroundColor = [UIColorwhiteColor];
[self.window makeKeyAndVisible];
returnYES;
}
2、创建一个子类,为UITableViewController的子类,名为RootViewController,不需要同时创建.xib。此为程序一开始看到的画面
RootViewController.h中添加如下:
@property (nonatomic, strong) NSMutableArray *mutableArrayForRootView;
RootViewController.m中修改如下:
#import "RootViewController.h"
#import "SongViewController.h"
@synthesize mutableArrayForRootView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self setTitle:@"哈哈"];
self.mutableArrayForRootView = [[NSMutableArray alloc] init];
SongViewController *mySongViewController = [[SongViewController alloc] initWithStyle:UITableViewStylePlain];
mySongViewController.title = @"11111";
[self.mutableArrayForRootView addObject:mySongViewController];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numberOfSection = [mutableArrayForRootView count];
return numberOfSection;
}
- (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];
}
// Configure the cell...
UITableViewController *tableViewController = [self.mutableArrayForRootView objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = tableViewController.title;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewController *tableViewController = [self.mutableArrayForRootView objectAtIndex:indexPath.row];
[self.navigationController pushViewController:tableViewController animated:YES];
}
画面如下:
可以按照类似的方法,在table中添加多个cell。
3、 创建一个子类,为UITableViewController的子类,名为SongViewController,不需要同时创建.xib。此视图为在root视图中,点击了table中的一个cell之后,看到的视图,
SongViewController.h文件添加如下:
@property (strong, nonatomic) NSArray *arrayOfSongViewController;
SongViewController.m文件修改如下:
#import "SongViewController.h"
#import "DetailViewControllerOfSong.h"
@implementation SongViewController
@synthesize arrayOfSongViewController;
- (void)viewDidLoad
{
[superviewDidLoad];
self.arrayOfSongViewController = [[NSArrayalloc]initWithObjects:@"aaa",@"bbbb",@"ccc",nil];
}
#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.arrayOfSongViewControllercount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SongViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.arrayOfSongViewController objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewControllerOfSong *detailView = [[DetailViewControllerOfSong alloc]initWithNibName:@"DetailViewControllerOfSong" bundle:nil];
detailView.chooseMessage = [self.arrayOfSongViewController objectAtIndex:indexPath.row];
detailView.title = [self.arrayOfSongViewController objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailView animated:YES];
}
画面如下:
4、创建一个UIViewController子类,名称为DetailViewControllerOfSong,可以同时创建.xib。
DetailViewControllerOfSong.h文件修改如下:
#import <UIKit/UIKit.h>
@interface DetailViewControllerOfSong : UIViewController
@property (nonatomic, strong) NSString *chooseMessage;
@end
DetailViewControllerOfSong.m文件修改如下:
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"message" message:self.chooseMessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
[alertView show];
}