0

向きが変わったときにアプリケーションが横向きまたは縦向きに対応するペン先を自動的にピックアップするようにペン先名を指定する方法はありますか?そうでない場合、アプリのデリゲート メソッドで方向の変更のために addObserver を試行していますが、正常に動作していますが、orientationChanged メソッドでペン先を変更すると、アプリがクラッシュし、次のようなエラーが発生します。

 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FRAppDelegate 0xa18bf20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key __mapView.'

私のコードは次のとおりです。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.


    self.viewController = [[FRViewController alloc] initWithNibName:@"FRViewController" bundle:nil];


    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(orientationChanged:)
     name:UIDeviceOrientationDidChangeNotification
     object:[UIDevice currentDevice]];

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) orientationChanged:(NSNotification *)note
{
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
        {
           NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewController" owner:self options:nil];
           [self.viewController setView:[nibArray objectAtIndex:0]];
            NSLog(@"device orientation comes to portrait mode ");
            /* start special animation */
        }
            break;

        case UIDeviceOrientationLandscapeRight:
        case UIDeviceOrientationLandscapeLeft:
        {
            NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewControllerLand" owner:self options:nil];
            [self.viewController setView:[nibArray objectAtIndex:0]];
            NSLog(@"device orientatino comes to Landscape mode");
            /* start special animation */
        }
            break;

        default:
            break;
    };
}
4

2 に答える 2

0

これは nib をロードするための直接的な解決策ではありませんが、2 番目の nib には UI オブジェクト (つまり__mapView) が適切にセットアップされていないため、このエラーが発生していると思います。つまり、xib から __mapView を接続した場合、IBOutlet はそのクラスに存在しなくなります。

更新: これは、ペン先を として保持FRAppDelegateしてロードしているために発生していますowner( として指定selfしていownerます。) に変更するとself.viewController、例外はなくなります。

すなわち; それ以外の :

NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewController" owner:self options:nil]; 
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewControllerLand" owner:self options:nil];

使用する

NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewController" owner:self.viewController options:nil];
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewControllerLand" owner:self.viewController options:nil];
于 2013-03-05T07:19:06.737 に答える
0
  1. 本当に別のペン先をロードしたい場合は、次を使用できます。

    [[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone" owner:self options:nil];
    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone"
                                                  owner:self
                                                options:nil];
    PageView_iPhone* myView = [nibViews objectAtIndex:0];
    

次に、ビューを新しいビューと交換する場所で

お気に入り

self.view = myView;
  1. または、Quoted By @Javy を使用できます

初期化中に、ビューをセットアップ (プログラムで作成) し、ビュー コントローラーの各ビューの中心位置用に追加の CGPoint を作成するか、フレーム用に CGRect を作成します。

ビューが init で作成された後、layoutViewsForOrientation という名前のメソッドを呼び出して初期位置を設定し、現在のステータス バーの向きを渡します。向きが変わると、次のコードを使用します。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    BOOL result = NO;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        result = (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        result = YES;
    }
    if( result == YES ) [self layoutViewsForOrientation:interfaceOrientation];
    return result;
}
于 2013-03-05T06:58:36.410 に答える