如何在不丢失UISwipeGestures的情况下调用loadNibNamed?

时间:2012-02-14 23:47:54

标签: iphone objective-c cocoa-touch xib uiinterfaceorientation

我正在加载一个不同的xib,当用户翻转到横向并且效果很好时,但我注意到我的滑动事件没有注册。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if ([self currentlyInLandscapeMode:toInterfaceOrientation]) {
        [[NSBundle mainBundle] loadNibNamed:@"PhotosLandscape" owner:self options:nil];
    }else{
        [[NSBundle mainBundle] loadNibNamed:@"PhotosPortrait" owner:self options:nil];   
    }
}

- (BOOL)currentlyInLandscapeMode:(UIInterfaceOrientation)interfaceOrientation
{
    return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

如何切换xib并保持我之前的视图/ xib中的所有状态?

更新

结果证明我的IBOutlets仍在工作,但我的滑动未注册

2 个答案:

答案 0 :(得分:2)

您无法使用nib文件布置现有对象。 nib文件存储为归档对象图,因此当您使用NSBundle的{​​{1}} loadNibNamed:UIViewController initWithNibName:加载一个笔尖时,一组新的对象被实例化。

解决这个问题的唯一方法是使用loadNibNamed来实例化一组新对象,并使用他们的frame属性为现有对象设置frame s,这不是这是一个很好的解决方案。

答案 1 :(得分:0)

原来我只需要在每个笔尖加载后重新注册滑动事件(如此)

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if ([self currentlyInLandscapeMode:toInterfaceOrientation]) {
        [[NSBundle mainBundle] loadNibNamed:@"VotePhotosLandscape" owner:self options:nil];
    }else{
        [[NSBundle mainBundle] loadNibNamed:@"VotePhotosViewController" owner:self options:nil];   
    }
    [self wireupSwipeEvents];
}

- (BOOL)currentlyInLandscapeMode:(UIInterfaceOrientation)interfaceOrientation
{
    return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

- (void)wireupSwipeEvents
{
    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release]; 
}