ios - 一些 IBOutlet 指向 nil

标签 ios xcode xib null iboutlet

过了很长一段时间,我终于再次开始使用 Objective-C,这是迄今为止我唯一完整的应用程序。目标是最终针对 ARC 和可能的 Storyboard进行重构,但目前我正在完善 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 的文件相同)。操作按预期触发,并且可以访问 socket ,但只能在 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 之前调用它,甚至在解雇:方法中也尝试这样做,但它们永远不可用。

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

任何提示将不胜感激!

提前致谢。

最佳答案

您曾经有过适用于 iPad 和 iPhone 的单独 .xibs 吗?如果是这样,您可能会遇到与我相同的问题。直到 @AliSoftware 向我透露了一些知识:

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

本质上你需要:

  1. 删除构建产品目录
  2. 从所有模拟器中删除您的应用
  3. 构建并运行

我认为问题是 iPad 的幽灵 .xib 挂起导致问题。真正将应用程序清除干净就达到了目的。祝你好运!

关于ios - 一些 IBOutlet 指向 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9202714/

相关文章:

c++ - 我如何为 tr1::unordered_map 定义一个不绑定(bind)模板参数的宏/typedef/etc?

ios - Xcode 6 和 iOS 7.1 模拟器 : Unable to boot the iOS Simulator

ios - iOS 上带页面控件的导航栏

ios - 自动布局 View xib - 没有可编辑的约束?我需要居中元素

ios - 在应用启动时打开大型json文件,迫使启动时间较长

iphone - UITableViewCell subview 直到重绘才显示

ios - respondsToSelector 无法识别已实现的选择器

ios - NSSortDescriptor 按最后一条消息时间戳对对话进行排序

ios - 添加到 UITextField 的 CALayer 不会出现在 xib View 上

ios - 从 XIB 到 Storyboard