0

4 つの異なる xib を使用するアプリがあります。それらを 1 ~ 4 と呼びましょう

したがって、ビュー 1 から開始し、ボタンを押すとビュー 2 に移動し、ビュー 2 には戻るボタン (1 に移動) と進むボタンがあり、3 に移動します。

とにかく、次のページ ボタンを削除し、ボタンを押す代わりにスワイプ コントロールを追加しました。次のページにスワイプできます。

ただし、スワイプを使用してタグ付きビューを呼び出す方法を知る必要があります。

現時点では、次のページの UIButton はタグ 1 として IB に設定されています。

これは私のスワイプ コードです (これは 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 にスワイプするように指示できますか?

助けていただければ幸いです:)

ありがとう、

クリス

---- FAOロブを更新。

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

改訂されたコード サンプルを見ると、すべてが同時に読み込まれる 5 つの異なるビュー コントローラー間の遷移に使用される [sic] というUIAppDelegateメソッドがあることが明らかです。swicthViewこの構造を考えると、5 つのページのどれが読み込まれたかを追跡し、左または右のスワイプに基づいて、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];
}

ジェスチャを削除しないことに注意してください (このビューに戻ったときにジェスチャが本当に必要ない場合を除きます)。また、コントローラーを作成し、提示し、リリースしていることにも注意してください。

数値はビューのサブビューを識別するために使用され、View Controller などを識別するためには使用されないためtag、このコンテキストでのプロパティへの繰り返しの参照を理解していません。tag「次のページの UIButton は IB でタグ 1 として設定されています」と言うと、後で「タグ 1 にスワイプするように指示できますか?」と尋ねます。「ボタンにスワイプ」するのは意味がありません。ただし、ボタンの 2 つのハンドラー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、モーダル遷移の推奨される方法。
  • presentModalViewController5.0 より前の iOS バージョンとの下位互換性が必要な場合に使用する、現在は非推奨の方法です。
  • 変数とメソッドの命名に関するアドバイスについては、Cocoaのコーディング ガイドラインの命名の基本。変数は通常小文字で始まり、クラスは通常大文字で始まることに注意してください。
于 2013-01-23T16:29:20.270 に答える