来自另一个类的pushViewController

发布于 2024-12-10 05:18:24 字数 2076 浏览 0 评论 0原文

我想从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-12-17 05:18:24

当您通过以下方式分配操作时:

[book1UIButton addTarget:self action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];

您说(当前对象:selfBookScrollViewController *self将响应UIControlEventTouchUpInside的事件book1UIButton。这意味着当用户点击按钮时,将调用 BookScrollViewController 类对象的 viewBookDetails 方法。

正如您在问题中提到的,您已经在 FirstViewController 中定义并实现了此类方法。

现在您应该

  1. 在 BookScrollViewController 中实现该方法,该方法会将新控制器推送到导航堆栈上,或者
  2. 设置另一个将响应该按钮事件的对象,该对象可以是 FirstViewController 类,例如,[book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];

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. That means when user tap on button method viewBookDetails of object of class BookScrollViewController will be called.

As you mentioned in your question you have defined and implemented such method in FirstViewController.

Now you should

  1. implement that method in BookScrollViewController that will push new controller onto navigation stack, or
  2. set another object that will respond on that button event, that object can be of class FirstViewController, for example, [book1UIButton addTarget:self.firstViewController action:@selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
私藏温柔 2024-12-17 05:18:24

当你这样做时:

FirstViewController *fvc = [[FirstViewController alloc] init];

显然你创建了 FirstViewController 的一个新实例。该新实例不在 UINavigationController 中,因此您无法将新的 viewController 推送到其 navigationController 属性上。

您可能想要做的是引用 FirstViewController 的现有实例并调用它的方法,而不是创建它的新实例。

When you do this:

FirstViewController *fvc = [[FirstViewController alloc] init];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文