7

私は2つのView Controllerを持っています。2番目のviewControllerはすべての向きをサポートする必要がありますが、1番目のviewcontrollerはポートレートモードのみにしたいです。私を助けてください。

AppDelegate クラスでは、私のコードは次のとおりです。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = self.viewController;
  //  [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];   
    return YES;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSLog(@"supportedInterfaceOrientationsForWindow");
    return  UIInterfaceOrientationMaskAllButUpsideDown;
}

最初の ViewController コードは次のとおりです。

-(BOOL)shouldAutorotate
{
    return NO;
}


-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
// Tell the system which initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationMaskPortrait;
}

2 番目の ViewController コードは次のとおりです。

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

// Tell the system which initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

私が調べたのは、「shouldAutorotate」メソッドが 1 番目と 2 番目の ViewController に対して呼び出されていないことです。

あなたの迅速な助けは非常に高く評価されます。ありがとう。カシフ

4

3 に答える 3

8

次のように、2番目の UIViewController の supportedInterfaceOrientations を設定してみてください。

- (BOOL) shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight; // add any other you want
}

また、最初の UIViewController で同じメソッドで縦のみを有効にします。

2nd UIViewController がサポートする必要があるプロジェクト設定でも、これらの向きを有効にする必要があるかもしれません。


[編集 #1: サンプル アプリケーションを追加]

ここで、うまくいけば、問題を解決するサンプルアプリケーションに行きます。

AppDelegate.h

#import <UIKit/UIKit.h>

@class FirstViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) FirstViewController *viewController;

@end

AppDelegate.m

#import "AppDelegate.h"

#import "FirstViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

- (IBAction)goToSecondViewController:(id)sender;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

- (IBAction)goToSecondViewController:(id)sender
{
    SecondViewController *svc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self presentViewController:svc animated:NO completion:nil];
}

@end

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

- (IBAction)goToFirstViewController:(id)sender;

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

- (IBAction)goToFirstViewController:(id)sender
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

@end

FirstViewController.xib

FirstViewController.xib

SecondViewController.xib

SecondViewController.xib

于 2012-09-20T07:48:49.213 に答える
2

あなたの説明に従って、最初のView ControllerがNavigation Controllerにプッシュされました。shouldAutorotateとのメソッド定義supportedInterfaceOrientationsは完全に正しいです。向きの変更中、ナビゲーション コントローラーの shouldRotate のみが起動され、最初のビュー コントローラー (ナビゲーション コントローラーの子) の shouldAutorotate は起動されません。したがってcategory、View Controllerまたは別のファイルでnavigationControllerを作成し、必要な場所にインポートする必要があります。UINavigation コントローラー カテゴリのコードは次のとおりです。

.h ファイルにはこのコードがあります

    @interface UINavigationController (autoRotate)

-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;

@end

.m ファイルにはこのコードがあります

  #import "UINavigationController+autoRotate.h"

@implementation UINavigationController (autoRotate)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

別のファイルにカテゴリを記述する場合は、最初と 2 番目のビュー コントローラーに .h ファイルを次のようにインポートする必要があります。#import "UINavigationController+autoRotate"

于 2013-03-29T06:36:25.670 に答える
0

簡単な解決策:

埋め込む :- (NSUInteger)supportedInterfaceOrientations

AppDelegate.m から尋ねられたとき

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

詳細については、すでにここで回答しています: iOS 6 の横向きと縦向き

于 2013-02-05T09:56:13.590 に答える