I want to push a view controller from my FirstViewController
to load BookDetailsViewController
. here's the setup I currently have.
// FirstViewController:(this is inside a uinavigationcontroller)
-(IBAction)viewBookDetails:(id)sender
{
NSLog(@"woo");
BookDetailsViewController *bdvc = [[BookDetailsViewController alloc] init];
[self.navigationController pushViewController:bdvc animated:YES];
}
==============================================================
// BookScrollViewController: (where the button is located)
[book1UIButton addTarget:self action:@selector(viewBookDetails:)
forControlEvents:UIControlEventTouchUpInside];
-(void)viewBookDetails:(id) sender
{
FirstViewController *fvc = [[FirstViewController alloc] init];
[fvc viewBookDetails:sender];
}
==============================================================
//how BookScrollViewController is created
BookScrollViewController *controller = [bookViewControllersArray
objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
NSString *bookTitle = @"ngee";
controller = [[BookScrollViewController alloc]initWithBook:bookTitle
imageNamesArray:imgDataArray pageNumber:page totalResult:[newReleasesArray
count]];
[bookViewControllersArray replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = bookScroll.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[bookScroll addSubview:controller.view];
}
when I tap the button in BookScrollViewController
it calls the IBAction
I have in FirstViewController
coz it prints my nslog but it's not loading pushing the BookDetailsViewController
to the navigation stack.
I tried assigning a button from FirstViewController
to call the IBAction
and it loads just fine. So, how can I successfully call the IBAction
from FirstViewController
using the button from BookScrollViewController
?
thanks!
When you do this:
you obviously create a new instance of FirstViewController. That new instance is not in a UINavigationController, so you can't push a new viewController onto its navigationController property.
What you probably want to do is reference the existing instance of FirstViewController and call the method on it instead of creating a new instance of it.
When you are assigning an action in the following way:
You say that (current object:
self
)BookScrollViewController *self
will respond to eventsUIControlEventTouchUpInside
ofbook1UIButton
. That means when user tap on button methodviewBookDetails
of object of classBookScrollViewController
will be called.As you mentioned in your question you have defined and implemented such method in
FirstViewController
.Now you should
BookScrollViewController
that will push new controller onto navigation stack, orFirstViewController
, for example,[book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];