一些指向零的IBOutlets

时间:2012-02-08 22:47:29

标签: ios xcode xib null iboutlet

很长一段时间后,我终于开始使用Objective-C,到目前为止我唯一的完整应用程序。目标是最终重构ARC和可能的故事板,但目前我正在抛出iOS 4.x目标。我很生疏,如果这是一个愚蠢的问题,请跟我说吧。

我的主视图中有一个触发showAction方法的按钮。方法如下:

- (void)showAbout
{
    AboutViewController *aboutView = [[[AboutViewController alloc] init] autorelease];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        aboutView.modalPresentationStyle = UIModalPresentationFormSheet;
    }
    else
    {
        aboutView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    }
    [self presentModalViewController:aboutView animated:YES];
}

AboutViewController类有这个.h:

@interface AboutViewController : UIViewController <UIActionSheetDelegate, MFMailComposeViewControllerDelegate> {
}

- (IBAction)dismiss:(UIButton *)sender;
- (IBAction)feedback:(UIButton *)sender;

@property (retain) IBOutlet UIButton *dismissButton;
@property (retain) IBOutlet UIButton *feedbackButton;
@property (retain) IBOutlet UIImageView *background;
@property (retain) IBOutlet UITextView *textView;

@end

并且.xib文件中的所有内容都已正确连接(适用于iPhone和iPad的文件)。这些操作按预期触发,并且可以访问插座,但仅限于iPhone。在iPad上运行时,只有textView被正确初始化,所有其余的都指向nil。

在.m中我试过这个:

@implementation AboutViewController

@synthesize dismissButton = _dismissButton;
@synthesize feedbackButton = _feedbackButton;
@synthesize background = _background;
@synthesize textView = _textView;

// ...

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSLog(@"obj self.dismiss: %@", self.dismissButton);
    NSLog(@"frame dismiss: %@", NSStringFromCGRect(self.dismissButton.frame));
}

在iPhone上我得到了这个:

2012-02-08 22:51:00.341 myapp[17290:207] obj self.dismiss: <UIButton: 0x70281d0; frame = (180 410; 120 30); opaque = NO; autoresize = LM+W+RM+TM; layer = <CALayer: 0x7028260>>
2012-02-08 22:51:00.342 myapp[17290:207] frame dismiss: {{180, 410}, {120, 30}}

在iPad上,我得到了这个:

2012-02-08 22:51:40.428 myapp[17320:207] obj self.dismiss: (null)
2012-02-08 22:51:40.428 myapp[17320:207] frame dismiss: {{0, 0}, {0, 0}}

根据UI惯例,没有其他任何事情发生,我真的对此感到困惑。就好像在iPad上没有设置除textView以外的任何东西的插座,即使它们在.xib文件中正确链接。在viewWillAppear中,self.textView 在iPad上也按预期工作。我也尝试在viewDidLoad中执行此操作,如果我没有弄错,应该在viewWillAppear之前调用它,甚至在dismiss:方法中,但它们永远不可用。

我需要访问按钮框架的原因是about视图相对复杂,无法自动重新定位其子视图。我唯一需要做的就是在iPad的较大模态视图上使按钮更宽。

任何提示都将不胜感激!

提前致谢。

1 个答案:

答案 0 :(得分:2)

你有没有为iPad和iPhone单独的.xibs?如果是这样,你可能会遇到同样的问题。直到@AliSoftware放弃了一些关于我的知识:

UIView in nib is nil on iPad, and not nil on iPhone

基本上你需要:

  1. 删除构建产品目录
  2. 从所有模拟器中删除您的应用
  3. 构建并运行
  4. 我认为问题在于,用于iPad的幽灵.xib会出现问题。真正擦拭应用程序干净的伎俩。祝你好运!