views:

421

answers:

2

I am doing a book kinda application using a viewBased application. The mainView acts as the backgroundView for loading pages. Each page is a different view and have its own nib. When i swipe on a page(actually on the mainView which have loaded the current view/page), that page will be removed and the next/previous page will be added.

[currentPage.view removeFromSuperview];
[self.view insertSubview:nextPage.view atIndex:0];

I have added a popoverController with a barbutton in this mainView. Its intialized with a tableviewController class named popClass. PopClass is another UITableViewController class which will act as the parent for the popViewController.

self.myPopController = [[PopController alloc] initWithStyle:UITableViewStylePlain];
self.myPopOverController = [[UIPopoverController alloc] initWithContentViewController:myPopController];

When I select certain row in the popover, I want the backgroundview(mainView) to load the page corresponding to the number of the row.

For that I have written a function in bookView(mainView) Controller class. And is calling it from the popOver's parent class when i select certain row.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger toPageNumber = indexPath.row + 1;
[viewController changeToPage:toPageNumber];
}

Function and the lines of code are executing but nothing is happening. Even the log lines are printed but no action takes place. The code is the same for removing a page and adding another page which is working successfully when i swipe the view/page.

What is the problem? OR is there anyother way to make the view change in mainView by using the popover?

A: 

It's hard to tell what's going wrong without looking at the code. That said, here's what I'd do:

(1) When a row is selected in mainView, present the popoverController modally:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PopoverViewController *vc = [[PopoverViewController alloc] init];
    [self.navigationController presentModalViewController:vc animated:YES];        
    [vc release];
}

(2) When something changes in the popoverController, for example a row is selected, set a value in the parentViewController (which should be the MainView):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.parentViewController.value = someValue;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

(3) Dismiss the popoverController where-ever you choose by calling:

[self dismissModalViewControllerAnimated:YES]`
conmulligan
I have edited the code and added details. Can u go thru??
wolverine
I wouldn't refer to the parentViewController like that. Instead, I would use a delegate pattern or the notification system. This way, the classes are much more reusable and loosely coupled.
Rengers
I used delegate earlier in a sample app for this same purpose and it was generating the same result. thats why i went the hard way. I will try it in this app and check whether its working correctly.
wolverine
Now its working fine and perfectly. Thanks a lot. If u hadnt mentioned about the delegate, i wudnt have dared to try it again.
wolverine
@Rengers, yes, you're absolutely right, I'd also use delegates wherever possible. I just used [UINavigationController parentViewController] as an rudimentary example of how to pass data between the two View Controllers.
conmulligan
+1  A: 

You need to have refernce of the background view controller in the popover controller.

In the PopController controller you should have a delagate like:

@interface PopController: UITablViewController{

  //// other varibales;
      id delegate;
}

@property (nonatomic, retain) id delgate;

Now from the class in which you are showing the popovercontroller

you should add this line:

[myPopController setDelegate:self];

Then you can easily acces any method of the main view controller class. By using

[delegate callTheRequiredMethod];

Hope this helps.

Thanks,

Madhup

Madhup
This one gives me warning when i try [delegate callTheRequiredMethod]. Says that callTheRequiredMethod not found. But i have there.
dombesz
@dombesz : have you declared the prototype in the .h file. If you have already added that then just add a type cast to the delegate variable.
Madhup