0

有没有办法指定笔尖名称,以便应用程序在方向发生变化时自动拾取横向或纵向的相应笔尖。就像 iphone 4 和 iphone 5 有 Default@2x.png 和 Default-568h@2x.png 之类的那。如果不是?,我正在尝试在应用程序委托方法中添加观察者以更改方向,它工作正常,但是在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
  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. 或者您可以使用 @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 回答
0

虽然这不是加载笔尖的直接解决方案,但我认为您遇到了该错误,因为您的第二个笔尖没有__mapView正确设置 UI 对象的出口(即)。即,无论您从 xib 连接到 __mapView 的何处,该类中都不再存在 IBOutlet。

更新: 发生这种情况是因为您正在加载保持FRAppDelegateowner( 您指定selfowner.) 的笔尖。如果您将其更改为self.viewController,那么您的异常应该会消失。

IE ; 代替 :

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 回答