来自另一个类的pushViewController
我想从我的 FirstViewController
推送一个视图控制器来加载 BookDetailsViewController
。这是我目前的设置。
// 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
到导航堆栈。
我尝试从 FirstViewController 分配一个按钮来调用 IBAction,它加载得很好。那么,如何使用 BookScrollViewController
中的按钮成功地从 FirstViewController
调用 IBAction
呢?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您通过以下方式分配操作时:
您说(当前对象:
self
)BookScrollViewController *self
将响应UIControlEventTouchUpInside
的事件book1UIButton
。这意味着当用户点击按钮时,将调用BookScrollViewController
类对象的viewBookDetails
方法。正如您在问题中提到的,您已经在 FirstViewController 中定义并实现了此类方法。
现在您应该
[book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
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];
当你这样做时:
显然你创建了 FirstViewController 的一个新实例。该新实例不在 UINavigationController 中,因此您无法将新的 viewController 推送到其 navigationController 属性上。
您可能想要做的是引用 FirstViewController 的现有实例并调用它的方法,而不是创建它的新实例。
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.