[英]pushViewController from another class
I want to push a view controller from my FirstViewController
to load BookDetailsViewController
. 我想从
FirstViewController
推送视图控制器以加载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. 当我点击
BookScrollViewController
的按钮时,它将调用我在FirstViewController
拥有的IBAction
, FirstViewController
它会打印我的nslog,但未加载将BookDetailsViewController
推BookDetailsViewController
导航堆栈。
I tried assigning a button from FirstViewController
to call the IBAction
and it loads just fine. 我尝试从
FirstViewController
分配一个按钮来调用IBAction
,它加载得很好。 So, how can I successfully call the IBAction
from FirstViewController
using the button from BookScrollViewController
? 因此,如何使用
FirstViewController
中的按钮从BookScrollViewController
成功调用IBAction
?
thanks! 谢谢!
When you are assigning an action in the following way: 通过以下方式分配操作时:
[book1UIButton addTarget:self action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
You say that (current object: self
) BookScrollViewController *self
will respond to events UIControlEventTouchUpInside
of book1UIButton
. 您说(当前对象:
self
) BookScrollViewController *self
将响应book1UIButton
事件UIControlEventTouchUpInside
。 That means when user tap on button method viewBookDetails
of object of class BookScrollViewController
will be called. 这意味着当用户点击按钮方法
viewBookDetails
对象的BookScrollViewController
将被调用。
As you mentioned in your question you have defined and implemented such method in FirstViewController
. 正如您在问题中提到的那样,您已经在
FirstViewController
定义并实现了这种方法。
Now you should 现在你应该
BookScrollViewController
that will push new controller onto navigation stack, or BookScrollViewController
中实现该方法,该方法会将新控制器推入导航堆栈,或者 FirstViewController
, for example, [book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
FirstViewController
类,例如, [book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
When you do this: 执行此操作时:
FirstViewController *fvc = [[FirstViewController alloc] init];
you obviously create a new instance of FirstViewController. 您显然可以创建一个FirstViewController的新实例。 That new instance is not in a UINavigationController, so you can't push a new viewController onto its navigationController property.
该新实例不在 UINavigationController中,因此您不能将新的viewController推入其NavigationController属性。
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. 您可能想要做的是引用FirstViewController的现有实例并对其调用方法,而不是为其创建新实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.