繁体   English   中英

来自另一个类的pushViewController

[英]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拥有的IBActionFirstViewController它会打印我的nslog,但未加载将BookDetailsViewControllerBookDetailsViewController导航堆栈。

我尝试从FirstViewController分配一个按钮来调用IBAction ,它加载得很好。 因此,如何使用FirstViewController中的按钮从BookScrollViewController成功调用IBAction

谢谢!

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

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

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

正如您在问题中提到的那样,您已经在FirstViewController定义并实现了这种方法。

现在你应该

  1. BookScrollViewController中实现该方法,该方法会将新控制器推入导航堆栈,或者
  2. 设置另一个将对该按钮事件做出响应的对象,该对象可以属于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM