一些指向 nil 的 IBOutlets [英] A few IBOutlets pointing to nil

查看:66
本文介绍了一些指向 nil 的 IBOutlets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间后,我终于再次开始使用 Objective-C,这是迄今为止我唯一完整的应用程序.目标是最终重构 ARC 和可能的故事板,但目前我正在完善 iOS 4.x 目标.我很生疏,所以如果这是一个愚蠢的问题,请不要问我.

After a long while I've finally started working with Objective-C again, on my only complete app so far. The goal is to eventually refactor for ARC and possibly storyboards, but for the time being I'm polishing the iOS 4.x target. I am quite rusty, so bar with me if this is a stupid question.

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

I have a button in my main view that triggers the showAction method. The method is as follows:

- (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:

The AboutViewController class has this .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.

and everything has been connected properly in the .xib file (the same one for iPhone and iPad). The actions are triggered as expected, and the outlets are accessible, but only on iPhone. When running on iPad, only textView is properly initialized, and all the rest points to nil.

在 .m 我试过这个:

In the .m I have tried this:

@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 上我得到了这个:

On iPhone I get this:

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 上,我得到了这个:

On iPad, instead, I get this:

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: 方法中,但它们永远不可用.

There is nothing else that happens depending on the UI Idiom, and I'm really confused about this. It's as if on the iPad the outlets for anything other than textView are not set, even though they are correctly linked up in the .xib file. In viewWillAppear, self.textView is working as expected also on iPad. I also tried doing this in viewDidLoad, which should be called before viewWillAppear if I'm not mistaken, and even in the dismiss: method, but they are just never available.

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

The reason I need to access the buttons' frames is that the about view is relatively complex and fails to reposition its subviews automatically. The only thing I need to do is make the buttons wider on the larger modal view on iPad.

任何提示将不胜感激!

提前致谢.

推荐答案

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

Did you ever have a separate .xibs for iPad and iPhone? If so you might have the same issue I had. That is until @AliSoftware dropped some knowledge on me:

nib 中的 UIView 为零在 iPad 上,在 iPhone 上不是零

基本上你需要:

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

我认为问题在于 iPad 的幽灵 .xib 一直在导致问题.真正把应用程序擦干净就行了.祝你好运!

I believe the problem is that a ghost .xib for iPad was hanging around causing issues. Truly wiping the apps clean did the trick. Good luck!

这篇关于一些指向 nil 的 IBOutlets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆