0

我有一个非常奇怪的问题,我想我知道我为什么会得到它,但我似乎想不出解决办法。我确信这里没有编译器错误。由于我的代码错误,我遇到过很多这样的情况,所有这些都发生了。

基本上,我有一个从 UIViewController 继承的名为 UnlockKeyboardViewController 的视图控制器,以及一个名为 UnlockKeyboard 的自定义视图(直接从 UIView 继承)。UnlockKeyboard 的标题如下所示:

@interface UnlockKeyboard : UIView
{
    NSArray *buttons;
    NSArray *passcodeFields;

    UIImage *buttonBackgroundImage;
    UIImage *buttonBackgroundHighlightedImage;
    UIImage *middleButtonBackgroundImage;
    UIImage *middleButtonBackgroundImageHighlighted;
    UIImage *screenBackgroundImage;

    UIImage *infoViewContainerImage;
    UIImage *keypadViewContainerImage;
    UIImage *passcodeFieldsContainerImage;

    UIImage *infoViewImage;
    UIImage *passcodeViewImage;

    UIView *infoViewContainer;
    UIView *keypadViewContainer;
    UIView *passcodeFieldsContainer;

    UIView *infoView;
    UIView *keypadView;
    UIView *passcodeFieldsView;

    UIView *infoToDisplayView; //The view the programmer passes to show in infoView.
}
@property(nonatomic, retain)UIImage *buttonBackgroundImage;
@property(nonatomic, retain)UIImage *buttonBackgroundHighlightedImage;
@property(nonatomic, retain)UIImage *screenBackgroundImage;
@property(nonatomic, retain)UIImage *keypadViewBackgroundImage;
@property(nonatomic, retain)UIImage *infoViewContainerImage;
@property(nonatomic, retain)UIImage *keypadViewContainerImage;
@property(nonatomic, retain)UIImage *passcodeFieldsContainerImage;
@property(nonatomic, retain)UIImage *infoViewImage;
@property(nonatomic, retain)UIImage *passcodeViewImage;
@property(nonatomic, retain)UIView *infoToDisplayView;

//Properties for container views.
@property(nonatomic, retain)UIView *infoViewContainer;
@property(nonatomic, retain)UIView *keypadViewContainer;
@property(nonatomic, retain)UIView *passcodeFieldsContainer;

@end

到目前为止,UnlockKeyboardViewController 实现看起来像这样:

@implementation UnlockKeyboardViewController
-(id)init
{
    if((self = [super init]))
    {
        self.view = [[UnlockKeyboard alloc] init];
    }
    return self;
}

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

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.7 animations:^{
        self.view.keypadViewContainer.frame = CGRectMake(0, 261, 320, 200);
    } completion:^(BOOL finished){

    }];
}

-(void)dealloc
{
    [super dealloc];
}
@end

这是我的问题变得有趣。每当我尝试编译时,终端(这是一个越狱应用程序,所以没有 Xcode)给我以下错误:

In function ‘void __-[UnlockKeyboardViewController viewDidAppear:]_block_invoke_1(void*)’:
Segmentation fault: 11
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://developer.apple.com/bugreporter> for instructions.

但是,此错误仅在我输入此行时出现:

self.view.keypadViewContainer.frame = CGRectMake(0, 261, 320, 200);

如果我的动画块中没有那一行,编译器不会给我那个错误,它会编译得很好。虽然,不管我放这条线,我总是得到分段线 11。

我认为这可能与 UIView 没有名为 keypadViewContainer 的成员这一事实有关,尽管 UnlockKeyboard 具有该属性并且是 UIView 的子类。我相信这是因为编译器实际上无法看到 UIView 和 UnlockKeyboard 之间的类层次结构。

如果我是对的,我不知道如何解决这个问题。想了一会儿。任何帮助我解决这个问题的意见将不胜感激。

4

1 回答 1

0

loadView首先,您应该只在其方法中设置视图控制器的视图属性:

- (void)loadView
{
    self.view = [[UnlockKeyboard alloc] init];
}

这将触发其他方法,例如viewDidLoad期望对象已完全初始化的方法。view故事的寓意:在控制器要求您之前不要设置属性。

然后,在您的viewDidAppear:方法中,将属性封装view到您正在使用的特定子类中应该避免任何警告,并希望避免奇怪的编译器崩溃:

[UIView animateWithDuration:0.7 animations:^{
    UnlockKeyboard *ukView = (UnlockKeyboard *)self.view;
    ukView.keypadViewContainer.frame = CGRectMake(0, 261, 320, 200);

您编写的代码应该触发了编译器警告,因为该属性keypadViewContainer未在UIView(表达式的类型self.view)上定义。

无可否认,类型转换有点难看,但这是必要的,因为视图控制器是一个通用的父类并且不知道它正在控制什么样的视图。您可能会发现定义一个无需一直进行强制转换的属性很有用:

@property (nonatomic,readonly) UnlockKeyboard *unlockKeyboardView;

以及对应的方法:

- (UnlockKeyboard *)unlockKeyboardView
{
    return self.view;
}
于 2012-07-29T02:06:40.053 回答