0

我有一个使用 4 种不同 xib 的应用程序,我们称它们为 1-4

因此,您从视图 1 开始,如果您按下按钮,它将带您查看 2,在视图 2 上,您有一个后退按钮(将您带到 1)和前进按钮,将您带到 3 等等

无论如何,我正在删除下一页按钮,并添加了一个滑动控件而不是按下按钮,您可以滑动到下一页。

但是,我需要知道如何使用滑动来调用标记视图。

目前,下一页的 UIButton 在 IB 中设置为标签 1

这是我的刷卡代码(这是第 1 页,所以只有向左刷卡)

- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        Page2ViewController *UIViewController =
        [[Page2ViewController alloc] initWithNibName:@"Page2ViewController~ipad" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];

    }else{

        Page2ViewController *UIViewController =
        [[Page2ViewController alloc] initWithNibName:@"Page2ViewController" bundle:nil];
        [self presentModalViewController:UIViewController animated:YES];

        Page2ViewController *VC = [[Page2ViewController alloc] initWithNibName:@"Page2ViewController" bundle:nil];
        [self presentModalViewController:VC animated:YES];

        [self.view removeGestureRecognizer:[self.view.gestureRecognizers lastObject]];

        [VC release];
    }
}

在那个代码中,我可以告诉它滑动到标签 1 吗?

将不胜感激任何帮助:)

谢谢,

克里斯

---- 更新了粮农组织 Rob;

在 appdelegate.m

- (void)swicthView:(int)viewControllerIndex :(CGRect)viewRect {


        if (viewControllerIndex < 0 || viewControllerIndex > viewControllers.count) {
            //invalid index passed to function - do nothing
        }else{

            if (subViewForceUseNibSize == NO) {
                //pass the view frame size at runtime

                if (CGRectIsEmpty(viewRect) || viewControllerIndex == 0) {

                    //no frame size so force full screen
                    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
                        viewRect =CGRectMake(0, 0, 320, 480);
                    }else{
                        viewRect = CGRectMake(0, 0, 768, 1024);
                    }
                }

            }else{
                //force use the nib size, so reduce size of NIB to leave display of NIB main nib below
                viewRect = ((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.frame;
            }

        }



        //swicth our view
        if (viewControllerIndex == 0) {
            /*
             for (UIView *subview in window.rootViewController.view.subviews) {
             [window.rootViewController.view sendSubviewToBack:subview];
             }
             */

            for (int x = 1; x<[viewControllers count]; x++) {
                if (((UIViewController *)[viewControllers objectAtIndex:x]).view.superview != nil) {
                    [window.rootViewController.view sendSubviewToBack:((UIViewController *)[viewControllers objectAtIndex:x]).view];
                }
            }

            [window bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:0]).view];
            return;
        }

        if (((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.superview != nil) {
            ((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.frame = viewRect;
            [window.rootViewController.view bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:0]).view];
            [window.rootViewController.view bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view];
        }else{
            ((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.frame = viewRect;
            [window.rootViewController.view bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:0]).view];
            [window.rootViewController.view addSubview:((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view];


        }

    }
4

1 回答 1

0

查看修改后的代码示例,很明显有一个UIAppDelegate名为swicthView[sic] 的方法用于在五个不同的视图控制器之间进行转换,所有这些视图控制器都是同时加载的。鉴于此结构,建议您有一个属性来跟踪加载了五个页面中的哪一个,并根据向左或向右滑动调用swicthView以转换到该控制器。因此:

@interface ViewController ()
@property (nonatomic) NSInteger currentPage;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.currentPage = 0;

    UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
    gesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:gesture];
    [gesture release];

    gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
    gesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:gesture];
    [gesture release];

    // the rest of the viewDidLoad
}

- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)gesture {

    if (self.currentPage < 4)
    {
        ++self.currentPage;
        [UIAppDelegate swicthView:self.currentPage :CGRectZero];
    }
}

- (void)handleRightSwipe:(UISwipeGestureRecognizer *)gesture {

    if (self.currentPage > 0)
    {
        --self.currentPage;
        [UIAppDelegate swicthView:self.currentPage :CGRectZero];
    }
}

坦率地说,我强烈建议停用该swicthView设计,而是使用自定义容器视图控制器。如果您观看WWDC 2011 - 实现 UIViewController 容器,您将看到一个很好的介绍,介绍了保持视图控制器层次结构与视图层次结构同步的重要性,并看到一些自定义容器的实际演示。


下面提供的原始答案基于正在执行的原始代码片段presentViewController。事实证明,需要一个非常不同的解决方案,如上所述,但出于历史目的,我保留了原始答案:

原答案:

我假设你有以下类型的代码viewDidLoad

UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:gesture];

然后你的手势处理程序可能是:

- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)gesture
{
    NSString *nibName;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        nibName = @"Page2ViewController~ipad";
    else
        nibName = @"Page2ViewController";

    Page2ViewController *controller = [[Page2ViewController alloc] initWithNibName:nibName bundle:nil];

    // if supporting iOS versions earlier than 5.0, then you should use:
    //
    //     [self presentModalViewController:controller animated:YES];
    // 
    // otherwise you should use presentViewController as done below.

    [self presentViewController:controller animated:YES completion:nil];

    [controller release];
}

请注意,我不会删除该手势(除非您在返回此视图时真的不希望该手势出现,这是不太可能的)。另请注意,我正在创建控制器、呈现和发布。

我不理解您tag在这种情况下对属性的重复引用,因为数值tag用于识别视图的子视图,而不是用于识别视图控制器或类似的东西。所以你说“下一页的 UIButton 在 IB 中设置为标签 1”,然后你问“在哪里......我可以告诉它滑动到标签 1 吗?” “滑动到按钮”是没有意义的。但是,您可以拥有两个处理程序,即按钮IBAction(我将调用它onPressNextButton...我不知道您调用它)和handleLeftSwipe调用相同的方法,例如:

- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)gesture
{
    [self goToNextViewController];
}

- (IBAction)onPressNextButton:(id)sender
{
    [self goToNextViewController];
}

- (void)goToNextViewController
{
    NSString *nibName;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        nibName = @"Page2ViewController~ipad";
    else
        nibName = @"Page2ViewController";

    Page2ViewController *controller = [[Page2ViewController alloc] initWithNibName:nibName bundle:nil];
    [self presentViewController:controller animated:YES completion:nil];
    [controller release];
}

参考:

  • presentViewController,模态转换的首选方法。
  • presentModalViewController,如果您需要向后兼容 5.0 之前的 iOS 版本,则使用现在已弃用的方法。
  • Cocoa 编码指南中的命名基础知识,提供有关命名变量和方法的建议。注意变量通常以小写字母开头,类通常以大写字母开头。
于 2013-01-23T16:29:20.270 回答