简体   繁体   中英

XIB doesn't load… just shows a black screen

I'm creating a universal application in iOS 4.2 and above.

I have a nib called DataInputViewController_iPhone.xib . On the first view controller which comes up I have a button which should launch the class DataInputViewController . It does it, but it doesn't show the contents of the XIB file. It just shows a black screen.

Any ideas why this happens?

I have this code which calls the new view controller:

-(IBAction)clickedButton:(id)sender {
    if((UIButton *)sender == (UIButton *)startButton){
        NSString *nibName;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            nibName = @"DataInputViewController_iPhone";
        } else {
            nibName = @"DataInputViewController_iPad";
        }

        DataInputViewController *nextVC = [[DataInputViewController alloc] initWithNibName:nibName bundle:nil];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
    else if((UIButton *)sender == (UIButton *)otherButton){
        NSLog(@"clicked the other button");
    }
}

NSLogging in this method and in the init method of the new view controller tells me that the nibNameOrNil variable is pulled through correctly.

This is what the (standard) init method looks like in DataInputViewController.m :

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

Any ideas why this is happening? Or how to fix?

Thanks :)

You don't have to manually set up the nib name. Just name them DataInputViewController~iphone.xib and DataInputViewController~ipad.xib , then call [[DataInputViewController alloc] init] .

See iOS Supports Device-Specific Resources for more information.

I had the same problem. Just clean your project, close XCode, reboot your Mac and open project again. And don't forget delete app on simulator or device previously.

The only way I have found to fix this is to delete the nibs and class and then recreate them. Not particularly elegant, but it works ...

I had this same issue where I needed to load either iPad or iPhone .xib files for a utility view controller. It is a form so it had a lot of UILabel and UITextField elements that were linked as IBOutlets. I would construct the entire layout, link all of the elements, and it worked fine until when I built and ran it. I even took the advice posted on this thread and re-created both .xib's and the class files (a great deal of linking IBOutlets) with no success.

The problem was this. The root view cannot be @synthesized in the implementation of the class. As soon as I synthesized the base view, everything went black. By simply removing the "@synthesize view;" line, everything worked like a charm. When I presented my view controller everything was there again.

Here is the header file for the class with all of the IBOutlets:

    #import <UIKit/UIKit.h>

@interface SignUpFormViewController : UIViewController <UITextFieldDelegate, NSXMLParserDelegate>

@property (strong, nonatomic) IBOutlet UIView *rootView;
@property (weak, nonatomic) IBOutlet UILabel *signUpFormLabel;
@property (weak, nonatomic) IBOutlet UIButton *infoButton;
@property (weak, nonatomic) IBOutlet UILabel *enterEmailLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterEmailTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterPasswordLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterPasswordTextField;
@property (weak, nonatomic) IBOutlet UILabel *reEnterPasswordLabel;
@property (weak, nonatomic) IBOutlet UITextField *reEnterPasswordTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterFirstNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterFirstNameTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterLastNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterLastNameTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterPhoneNumberLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterPhoneNumberTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterStreetAddressLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterStreetAddressTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterStreetAddress2Label;
@property (weak, nonatomic) IBOutlet UITextField *enterStreetAddress2TextField;
@property (weak, nonatomic) IBOutlet UILabel *enterCityLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterCityTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterStateLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterStateTextField;
@property (weak, nonatomic) IBOutlet UILabel *enterZipLabel;
@property (weak, nonatomic) IBOutlet UITextField *enterZipTextField;
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UIButton *signUpNowButton;

- (IBAction)infoButtonPressed:(id)sender;
- (IBAction)backButtonPressed:(id)sender;
- (IBAction)signUpNowButtonPressed:(id)sender;

@end

Here is the implementation file with the root view being synthesized:

#import "SignUpFormViewController.h"

@implementation SignUpFormViewController
@synthesize rootView,signUpFormLabel,infoButton,enterEmailLabel,enterEmailTextField,enterPasswordLabel,enterPasswordTextField,reEnterPasswordLabel,reEnterPasswordTextField,enterFirstNameLabel,enterFirstNameTextField,enterLastNameLabel,enterLastNameTextField,enterPhoneNumberLabel,enterPhoneNumberTextField,enterStreetAddressLabel,enterStreetAddressTextField,enterStreetAddress2Label,enterStreetAddress2TextField,enterCityLabel,enterCityTextField,enterStateLabel,enterStateTextField,enterZipLabel,enterZipTextField,backButton,signUpNowButton,btnDone,btnPrev,btnNext,inputAccView,activeTextField,currentString;

Simply remove the "rootView" from the list of properties being synthesized and this should fix the problem. Much easier than deleting the .xib files for BOTH iPad and iPhone layouts and re-creating them.

FYI I am using xCode 4.6.3 with target SDK = 6.1

I hope this helps anybody else that experiences this time-consuming and very frustrating issue.

This may help you: delete your app on the simulator or device and run again. I find if I twiddle with nibs too much, especially around names or localization, it appears that everything works (I get no errors), but Xcode is actually ignoring the changes.

So perform a Clean ( Product->Clean ), delete from simulator or device, run again.

You have to make sure to implement the loadView method in your DataInputViewController.m and inside the method add the line [super loadView], like this:


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

I have found that not dragging an outlet from the File Owner to your custom sub-classed UITableViewCell.h or .m file can cause the blackness as well. Make sure that you drag an outlet to one of the two files associated with your sub-class of UITableViewCell.

In my case, I was receive xib file from co-worker. well loaded xib but did not shown. spent all my one a day because Hidden checkbox was checked

check Hidden checkbox in xib.

Simple Fix!! Navigating to version editor and back to standard editor fixed the problem for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM