另一个类的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
的button,它调用我在FirstViewController
的IBAction
,它会打印我的nslog,但不会加载将BookDetailsViewController
推BookDetailsViewController
导航堆栈。
我试图从FirstViewController
分配一个button来调用IBAction
,它加载得很好。 那么,如何使用FirstViewController
中的button从BookScrollViewController
成功调用IBAction
?
谢谢!
当您按以下方式分配操作时:
[book1UIButton addTarget:self action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
你说(当前对象: self
) BookScrollViewController *self
将响应book1UIButton
事件UIControlEventTouchUpInside
。 这意味着当用户点击viewBookDetails
类的对象的button方法BookScrollViewController
将被调用。
正如你在你的问题中提到的,你已经在FirstViewController
定义和实现了这样的方法。
现在你应该
- 在
BookScrollViewController
中实现该方法,该方法将新控制器推入导航栈,或者 - 设置另一个将响应该button事件的对象,该对象可以是类
FirstViewController
,例如,[book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
当你这样做:
FirstViewController *fvc = [[FirstViewController alloc] init];
你显然创build了一个FirstViewController的新实例。 该新实例不在 UINavigationController中,因此您无法将新的viewController推送到其navigationController属性中。
你可能想要做的是引用FirstViewController的现有实例,并调用它的方法,而不是创build一个新的实例。