I’m making Catalog like iOS application. using Xcode 4.2 with story board.
This application have two views, and each changeable by tab bar controller.
Application image is here. http://www.0502.me/help/xcode-cat.png
I made Catalog view, and Table of Contents(TableView).
But I cannot change views from Table of Contents to Catalog view.
I think when Table of Contents’s page number value clicked, Catalog view’s variable and view change will be good result.
First View is Catalog view.
It’s controlled by FirstViewController, and for image scale change, PageView class also use.
Second view is table of contents, controlled by ListViewController. it’s parse plist(xml), and manipulate cell values(Title and Page number).
Code is below, please help.
FirstViewController
@implementation TableOfContentSecondViewController
@synthesize currentPage;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)pageLoad:(UIScrollView *)scrollView {
currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
NSLog(@"CurrentPage- %i",currentPage);
NSLog(@"CurrentOffSet- %f",scrollView.contentOffset.x);
int pageWidth = self.view.frame.size.width;
int pageHeight = self.view.frame.size.height;
NSLog(@"pageWidth- %i",pageWidth);
prevPage.frame = CGRectMake(
pageWidth * (currentPage - 1),
0,
pageWidth,
pageHeight
);
if (currentPage > 0) {
[prevPage setImage:[NSString stringWithFormat:@"%d", (currentPage - 1) % kPageNum]];
prevPage.hidden = NO;
}
else {
prevPage.hidden = YES;
}
currPage.frame = CGRectMake(
pageWidth * currentPage,
0,
pageWidth,
pageHeight
);
[currPage setImage:[NSString stringWithFormat:@"%d", currentPage % kPageNum]];
currPage.hidden = NO;
nextPage.frame = CGRectMake(
pageWidth * (currentPage + 1),
0,
pageWidth,
pageHeight
);
if(currentPage < (kPageNum -1)) {
[nextPage setImage:[NSString stringWithFormat:@"%d", (currentPage + 1) % kPageNum]];
nextPage.hidden = NO;
}
else {
nextPage.hidden = YES;
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.bounds;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * kPageNum,
self.view.frame.size.height
);
//NSLog(@"ContentSize- %@",NSStringFromCGSize(scrollView.contentSize));
scrollView.pagingEnabled = YES;
[self.view addSubview:scrollView];
scrollView.delegate = self;
prevPage = [[PageView alloc] initWithFrame:self.view.bounds];
[scrollView addSubview:prevPage];
currPage = [[PageView alloc] initWithFrame:self.view.bounds];
[scrollView addSubview:currPage];
nextPage = [[PageView alloc] initWithFrame:self.view.bounds];
[scrollView addSubview:nextPage];
[self pageLoad:scrollView];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat position = scrollView.contentOffset.x / scrollView.bounds.size.width;
CGFloat delta = position - (CGFloat)currentPage;
if (fabs(delta) >= 1.0f) {
[self pageLoad:scrollView];
}
//NSLog(@"ContentOffset- %f", scrollView.contentOffset.x);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end
PageView.m
@implementation PageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
imageView = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:imageView];
self.delegate = self;
self.minimumZoomScale = 1.0;
self.maximumZoomScale = 4.0;
}
return self;
}
-(void)adjustScrollView:(BOOL)animate {
[self setZoomScale:self.minimumZoomScale animated:animate];
}
-(void)setImage:(NSString *)image {
NSString *path = [[NSBundle mainBundle] pathForResource:image ofType:@"jpg"];
NSLog(@"Image- %@",image);
/*
TableOfContentSecondViewController *tcController = [[TableOfContentSecondViewController alloc] init];
tcController.currentPage = 120;
*/
imageView.image = [UIImage imageWithContentsOfFile:path];
[self adjustScrollView:NO];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if([touch tapCount] > 1) {
[self adjustScrollView:YES];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
SecondViewController is for Table of Contents.
if there is no child element, bring back Page number.
ListViewController.m
@implementation ListViewController
@synthesize tableDataSource,currentTitle,currentLevel;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
if(currentLevel == 0) {
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource = tempArray;
TableOfContentAppDelegate *AppDelegate = (TableOfContentAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
self.navigationItem.title = @"Table of Contents";
}
else
self.navigationItem.title = currentTitle;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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.tableDataSource 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];
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"Title"];
return cell;
// Configure the cell...
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
NSArray *Children = [dictionary objectForKey:@"Children"];
if([Children count] == 0) {
/* I want to Jump to selected page on scrollView */
}
else {
ListViewController *rvController = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
rvController.currentLevel += 1;
rvController.currentTitle = [dictionary objectForKey:@"Title"];
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
}
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end @implementation ListViewController
@synthesize tableDataSource,currentTitle,currentLevel;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
if(currentLevel == 0) {
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource = tempArray;
TableOfContentAppDelegate *AppDelegate = (TableOfContentAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
self.navigationItem.title = @"Table of Contens";
}
else
self.navigationItem.title = currentTitle;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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.tableDataSource 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];
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"Title"];
return cell;
// Configure the cell...
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
NSArray *Children = [dictionary objectForKey:@"Children"];
if([Children count] == 0) {
/* I want to Jump to selected page on scrollView */
}
else {
ListViewController *rvController = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
rvController.currentLevel += 1;
rvController.currentTitle = [dictionary objectForKey:@"Title"];
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
}
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
Perhaps this will help:
Coordinating Efforts Between View Controllers