pushViewController from another class

2019-09-01 03:51发布

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!

2条回答
来,给爷笑一个
2楼-- · 2019-09-01 04:26

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.

查看更多
We Are One
3楼-- · 2019-09-01 04:36

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];
查看更多
登录 后发表回答