nib 中的 UIView 在 iPad 上是 nil,在 iPhone 上不是 nil (UIView in nib is nil on iPad, and not nil on iPhone)


問題描述

nib 中的 UIView 在 iPad 上是 nil,在 iPhone 上不是 nil (UIView in nib is nil on iPad, and not nil on iPhone)

I have a weird problem with my app. I load a VC at runtime with a NIB named "LoginViewController." That NIB has a bunch of outlets to UIView objects that are placed in the NIB like so: 

At runtime I dynamically place those UIView objects as headers in my table view. Each separate UIView in this case (except for the one with the UITableView in it, on the right) is set as an IBOutlet like so. Each has a different name of course:

@property (strong, nonatomic) IBOutlet UIView *connectionInProgressView;

Now, in my VC's viewDidLoad method I run through all four of these separate views and ensure they exist by writing to the log:

NSLog(@"connection in progress view: %@", self.connectionInProgressView);

My problem is this: self.connectionInProgressView is nil on iPad and not nil on iPhone. All the other views regardless of platform are NOT nil and are instantiated. I'm running this in the simulator on iOS 5.0, 5.1 and 6.1. I can recreate the issue on all iPad simulators. My app works just fine and loads the view with the spinner on iPhone, but on iPad it can't load that view because it's nil! Why!?

I have tried the following:

  • Checking that my NIB's File's Owner points to the proper view
  • That the self.connectionInProgressView doesn't point to any other IBOutlet
  • Checking that all the other sub views are hooked up the same way as the offending view. They are.
  • Deleting self.connectionInProgressView and re‑creating it after removing all its NIB connections.
  • Removing the activity indicator from the view and loading just a view with a UILabel inside. No luck, its not the activity indicator.
  • Checked for any code in the VC that might reference self.connectionInProgressView. There is none (other than it's nil statement in viewDidUnLoad).
  • Checking the property attribute types on my AppDelegate properties of the UINavigationController and LoginViewController.
  • Retaining a copy of LoginViewController on AppDelegate, though this seems wasteful and didn't make a difference in the outcome.

[EDIT 2]

Question: How is the VC loaded 

I have a UINavigationController that is a property on my AppDelegate. That UINavigationController is alloc‑init'd in applicationDidFinishLaunchingWithOptions to which I pass my VC as a root view controller. I instantiate the VC at this point.

// Create login/start up views
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

self.loginViewNavigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    self.loginViewNavigationController.modalPresentationStyle = UIModalPresentationFormSheet;

Note that this is working on iPhone even though I'll present this UINavigationController later as a modal form sheet (not sure if that is contributing to my issue) on iPad.

Some time later in AppDelegate when a different delegate class throws an event I will present this UINavigationController, with my VC in it modally. I do that like this:

if (self.loginViewNavigationController != nil) {

    [self.window.rootViewController presentViewController:self.loginViewNavigationController animated:YES completion:nil];
}

I'm going bonkers trying to determine why this one particular view dragged onto my NIB and wired up the same as all the others is nil on an iPad and not nil on iPhone.

[EDIT 1] While searching for a duplicate question I failed to find this:

A few IBOutlets pointing to nil

I am not alone!

Any ideas?

‑‑‑‑‑

參考解法

方法 1:

As you are building an Universal app, are you sure you didn't create a separate NIB for iPhone and iPad (LoginViewController.xib and LoginViewController~ipad.xib typically)?

Or maybe you thought you have only one XIB but at some point you had a dedicated XIB for iPad that got compiled into a NIB and even if you deleted the XIB file in your project, the compiled NIB dedicated for iPad is still in the build products.

In any case, try to:

  • Clean your project, or even better, your Build Products Directory
  • To do the latter, you can delete your Derived Data folder ("Organizer" window ‑> "Projects" tab ‑> Select your project on the left ‑> click on the "Delete" button next to "Derived Data")
  • Delete your app from all your Simulators, just to be sure
  • Build and Run again

If this does not work, some ideas to help you debug and identify if the issue is related to the iPad using a different XIB or not:

  • You can try some tests like modifying another IBOutlet of your XIB, for example disconnect one of the other IBOutlet that are not nil from the view it was connected to in the XIB, so that when you run your app again it should be logged as nil this time. If it is nil on the iPhone Simulator (as expected as you just disconnected it)… but still non‑nil on the iPad Simulator, then it will definitely mean that you use a different XIB file for iPhone and iPad.
  • You can also change in the XIB the color of our of your UIView that is correctly connected to one of your IBOutlet, and see if the change is reflected in both iPhone and iPad or only on iPhone, so check that this XIB you are editing is the same used in iPad or not.

方法 2:

I had a similar issue, cleaned my project, deleted Derived Data, made sure I wasn't using separate xibs for iPad; nothing seemed to work.  Turns out my issue was super simple.  I was init‑ing my view controller like so:

// In Swift
let vc = MyViewController()

instead of using the nibName directly:

let vc = MyViewController(nibName: "MyViewController", bundle: NSBundle.mainBundle())

Since my nil‑outlet error only seemed to come up when testing on iPad, I never thought to check how the controller was initialized.

方法 3:

For this specific case, I faced a weird thing. I had a UITextField become a first‑response in viewDidLoad, and I had NSNotifications hooked to that UITextField to manage keyboard frame etc. But interestingly when the notification was dispatched in viewDidLoad, only on iPad, the outlets were nil (unlike iPhone). Weird.

(by AaronAliSoftwareWongWraySean Goudarzi)

參考文件

  1. UIView in nib is nil on iPad, and not nil on iPhone (CC BY‑SA 3.0/4.0)

#iboutlet #null #iOS #nib






相關問題

iOS - IB 視圖模式 (iOS - IB view mode)

NSWindow 的 IBOutlet 組件什麼時候初始化? (When IBOutlet components of NSWindow are initialized?)

收到內存警告和內存洩漏 (Receive memory warning and memory leak)

如何在 init 方法中從 NIB 加載 IBOutlets? (How to load the IBOutlets from NIB in the the init method?)

將 xib 從一個項目複製到另一個項目 (copy the xib from one project to another project)

將案例編碼到 appDelegate 文件中 (The case(s) coding into appDelegate file)

nib 中的 UIView 在 iPad 上是 nil,在 iPhone 上不是 nil (UIView in nib is nil on iPad, and not nil on iPhone)

awakeFromNib 無法從 UILabel 獲取值,也無法禁用 UIButton (awakeFromNib can't get value from UILabel and can't disable UIButton)

“找不到名為 ViewController 的類的任何信息” ("Could not find any information for class named ViewController")

在 Xcode 7 中將出口從文件連接到情節提要 (Connect outlet from file to storyboard in Xcode 7)

為什麼“文件所有者”有時會有“視圖”出口?我可以添加我自己的嗎? (Why does "File's Owner" sometimes have a "view" outlet? Can I add my own?)

Interface Builder 中的 IBOutletCollection 集排序 (IBOutletCollection set ordering in Interface Builder)







留言討論