0

So I've got a few UIViewControllers which I've subclassed that I'm trying to instantiate with nib files as views. The problem is that once I instantiate them, the view property on them is throwing an EXC_BAD_ACCESS error and crash the app goes.

Here's what I'm doing:

KMLFTPConnectionEditViewController.h

#import <UIKit/UIKit.h>
@interface KMLFTPConnectionEditViewController : UIViewController
@end

KMLFTPConnectionEditViewController.m

#import "KMLFTPConnectionEditViewController.h"

@interface KMLFTPConnectionEditViewController ()

@end

@implementation KMLFTPConnectionEditViewController

- (id)init
{
    self = [super initWithNibName:@"KMLFTPConnectionEditView" bundle:nil];
    if ( self ) {
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return [self init];
}

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

@end

Elsewhere

KMLFTPConnectionEditViewController * ftpvc = [[KMLFTPConnectionEditViewController alloc] init];
NSLog(@"View: %@", ftpvc.view); // <- crash!

Now, as for the nib, it's called KMLFTPConnectionEditView.xib, and it meets all the requirements I think it needs, as far as I can tell from other SO posts, as well as Apple's own docs:

  1. Its file's owner class is set to KMLFTPConnectionEditViewController
  2. Its file's owner's view outlet is connected to the primary view in the nib.
  3. The nib exists in the project, and it's included in the main application bundle.

This is the also the case with another UIViewController subclass I set up with a nib.

This was working for me this morning, but I must have made a change (there was an XCode update today...) which I'm just not seeing. I've been battling this for hours, to no avail.

Is there something simple I'm overlooking here?

UPDATE 1

It's too big to post inline here, but as per Aaron's request below, here is the crash log. It looks like something is recursing with the UITextField appearance proxy I've set up. Any ideas?

UPDATE 2

If I remove the UITextFields from the nib that I'm trying to load up, there's no problem. Once I put any UITextInput views back in the nib, even if they're not hooked up to any outlets or set up in any way, then I get this recursion crash.

Since it seems to be something going on with the appearance proxy and the UITextField, here's the method I've got set up in the AppDelegate to define appearances. Note that any non-standard colors have been defined in a UIColor+AppStyles category, which is imported into the AppDelegate.

- (void)customizeAppearance
{
    // Customize the navigation bar appearance
    UIImage *navbarBackgroundImage = [[UIImage imageNamed:@"background-navbar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UINavigationBar appearance] setBackgroundImage:navbarBackgroundImage forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:
              [UIColor primaryTextColor], UITextAttributeTextColor,
              [UIColor primaryTextShadowColor], UITextAttributeTextShadowColor,
              [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
              [UIFont titleHeaderFont], UITextAttributeFont,
              nil
          ]
     ];
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:1 forBarMetrics:UIBarMetricsDefault];

    // And the toolbar buttons
    UIOffset buttonTextOffset = UIOffsetMake(0, 2);
    UIImage *navbarButtonImage = [[UIImage imageNamed:@"background-navbar-button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)];
    UIImage *navbarBackButton = [[UIImage imageNamed:@"background-navbar-back-button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 2)];
    NSDictionary *buttonTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor invertedTextColor], UITextAttributeTextColor,
                                            [UIColor invertedTextShadowColor], UITextAttributeTextShadowColor,
                                            [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                                            [UIFont buttonFontSmall], UITextAttributeFont,
                                            nil
                                          ];

    // Text styles
    [[UIBarButtonItem appearance] setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];

    // Normal bar buttons
    [[UIBarButtonItem appearance] setBackgroundImage:navbarButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setTitlePositionAdjustment:buttonTextOffset forBarMetrics:UIBarMetricsDefault];

    // Back button
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:navbarBackButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:buttonTextOffset forBarMetrics:UIBarMetricsDefault];

    // And now for the tab bar
    UIImage *toolbarBackgroundImage = [[UIImage imageNamed:@"background-toolbar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UIToolbar appearance] setBackgroundImage:toolbarBackgroundImage forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];

    // Text Inputs  
    [[UITextField appearance] setBorderStyle:UITextBorderStyleNone];
    [[UITextField appearance] setBackgroundColor:[UIColor clearColor]];
    [[UITextField appearance] setFont:[UIFont textInputFont]];
    [[UITextField appearance] setTextColor:[UIColor invertedTextColor]];
    [[UITextField appearance] setTextAlignment:NSTextAlignmentRight];

    // And labels
    [[UILabel appearance] setFont:[UIFont formLabelFont]];
    [[UILabel appearance] setTextColor:[UIColor invertedTextColor]];

    [[KMLSectionHeaderLabel appearance] setFont:[UIFont titleHeaderFont]];
    [[KMLSectionHeaderLabel appearance] setBackgroundColor:[UIColor sectionHeaderBackgroundColor]];
}

UPDATE 3

So I've narrowed it down to one line of code in the appearance proxy method that is causing the crash:

[[UITextField appearance] setBackgroundColor:[UIColor clearColor]];

If I comment that out, all is well. It seems I don't even need that for what I want to do, but any ideas as to why that would be causing the recursion and crash? And more puzzlingly, why was this working just fine yesterday morning? I hadn't made any changes to that part of the appearance proxy code (I had tweaked the UILabel bits and added the KMLSectionHeaderLabel bits, though). I'd like to understand what's going on here.

Michael Bester
  • 315
  • 4
  • 11

0 Answers0