问题描述
我想从我的 FirstViewController
中推送视图控制器以加载 BookDetailsViewController
.这是我目前的设置.
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];
}
当我点击 BookScrollViewController
中的按钮时,它会调用 FirstViewController
中的 IBAction
,因为它会打印我的nslog,但不会加载推送 BookDetailsViewController
到导航堆栈.
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.
我尝试从 FirstViewController
分配一个按钮来调用 IBAction
,它加载得很好.因此,如何使用 BookScrollViewController
中的按钮从 FirstViewController
中成功调用 IBAction
?
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
?
谢谢!
推荐答案
执行此操作时:
FirstViewController *fvc = [[FirstViewController alloc] init];
您显然创建了一个FirstViewController的新实例.这个新实例不是UINavigationController中的 ,因此您不能将新的viewController推入其NavigationController属性中.
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.
您可能想做的是引用FirstViewController的现有实例并对其调用方法,而不是为其创建新实例.
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.
这篇关于来自另一个类的pushViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!