0

如何创建具有自定义返回类型的方法?

我基本上想返回“视图控制器”并进一步使用与界面构建器中的 IBAction 相同的方法并指向一个按钮......

我基本上想调用下面的方法或创建一个可以使用按钮调用的自定义方法......有办法吗?

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController 

{ 
    NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController labelContents]];
    if(currentIndex == self.modelArray.count-1)
    {
        return nil;
    }
    ContentViewController *contentViewController = [[ContentViewController alloc] init];
    contentViewController.labelContents = [self.modelArray objectAtIndex:currentIndex + 1];
return contentViewController; 

}
4

2 回答 2

2

您可以创建一个方法并将其连接到按钮

。H

 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController ;     
 -(IBAction)myMethod; //connect this to your button

.m

-(IBAction)myMethod
 {
    //assuming you want you want to push to returned view controller
    [self.navigationController pushViewController:[self pageViewController:viewController viewControllerAfterViewController:viewController2] animated:YES];        
 }

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController 
 { 
    NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController labelContents]];
    if(currentIndex == self.modelArray.count-1)
    {
    return nil;
    }

 ContentViewController *contentViewController = [[ContentViewController alloc] init];
 contentViewController.labelContents = [self.modelArray objectAtIndex:currentIndex + 1];
 return contentViewController; 
}
于 2012-07-06T05:33:46.330 回答
-2

IBAction 只是用作提示,它不是“绑定”。这意味着您可以通过以下几种方式之一做您想做的事:

  1. 以编程方式。使用 @selector(yourActionMethod:) 在类似 viewDidLoad 中设置按钮的操作

  2. “技巧”xcode;首先将方法设置为 IBAction,然后通过拖放进行连接,然后更改方法的声明/实现。它会工作的。

于 2012-07-06T04:37:54.590 回答