![](/img/trans.png)
[英]calling pushViewController from a inside a custom UITableViewCell class
[英]pushViewController from another class
我想从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
, FirstViewController
它会打印我的nslog,但未加载将BookDetailsViewController
推BookDetailsViewController
导航堆栈。
我尝试从FirstViewController
分配一个按钮来调用IBAction
,它加载得很好。 因此,如何使用FirstViewController
中的按钮从BookScrollViewController
成功调用IBAction
?
谢谢!
通过以下方式分配操作时:
[book1UIButton addTarget:self action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
您说(当前对象: self
) BookScrollViewController *self
将响应book1UIButton
事件UIControlEventTouchUpInside
。 这意味着当用户点击按钮方法viewBookDetails
对象的BookScrollViewController
将被调用。
正如您在问题中提到的那样,您已经在FirstViewController
定义并实现了这种方法。
现在你应该
BookScrollViewController
中实现该方法,该方法会将新控制器推入导航堆栈,或者 FirstViewController
类,例如, [book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
执行此操作时:
FirstViewController *fvc = [[FirstViewController alloc] init];
您显然可以创建一个FirstViewController的新实例。 该新实例不在 UINavigationController中,因此您不能将新的viewController推入其NavigationController属性。
您可能想要做的是引用FirstViewController的现有实例并对其调用方法,而不是为其创建新实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.