Why is my IBOutlet released under ARC?

Problem

IBOutlet is released before I have the opportunity to use it.

What I want

I want to access the navigation controller from my application delegate so that I can reload the table view.

My setting

I have:

  • A Main.xib, which is set as the main interface in the target settings
  • IBOutlet for navigation controller as ivar for my application delegate
  • This IBOutlet connects to the correct navigation controller in Main.xib
  • App Delegate is created in xib but not set as File Owner

I am using ARC, Xcode 4.3.2 and iOS5.1

What i tried

  • Change Deployment Goal
  • Putting a breakpoint on dealloc for the navigation controller, application delegate - they are never called
  • Reading everything I can find on ARC and IBOutlets - nothing contradicts what I do.
  • Creating a new project with the minimum required classes - I see exactly the same problem

the code

KPAppDelegate.h

@interface KPAppDelegate : UIResponder <UIApplicationDelegate> { IBOutlet KPBrowseExpensesNavigationController *nc; } @property (strong) IBOutlet KPBrowseExpensesNavigationController *nc; 

KPAppDelegate.m

 @implementation KPAppDelegate @synthesize nc; -(void)setNc:(KPBrowseExpensesNavigationController *)nc_ { nc = nc_; // This gets called on view load and nc gets set. } ...snip... // This is called about 5 seconds after app startup -(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects { // By the time we get here, nc is nil. UITableViewController *tvc = [[nc viewControllers] objectAtIndex:0]; [[tvc tableView] reloadData]; } @end 

UPDATE

I have to do something really stupid here. Even an incredibly simple project still shows this problem. See the link below.

Download a simple test project that shows the problem.

+6
source share
4 answers

In the nib window, set the FilesOwner class as UIApplication, and then specify the delegate from Outlets to the AppDelegate object. This is what is wrong in your sample project.

+2
source

Is your way out of the interface builder set as the type KPBrowseExpensesNavigationController ? If not, this will not create a connection between your nib and ViewController.

You must set your custom class as KPBrowseExpensesNavigationController in the Identity Inspector

+2
source

I'm not sure why you declare it as a property and a non-property. I should do something like this:

 @interface KPAppDelegate : UIResponder <UIApplicationDelegate> @property (nonatomic, strong) IBOutlet KPBrowseExpensesNavigationController *nc; 

And in your implementation:

 @implementation KPAppDelegate @synthesize nc = _nc; // So you don't accidentally use nc ...snip... // This is called about 5 seconds after app startup -(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects { // By the time we get here, nc is nil. UITableViewController *tvc = [[**self.nc** viewControllers] objectAtIndex:0]; [[tvc tableView] reloadData]; } @end 

Hope this helps!

+1
source

I have not seen where you assign your navigation controller. Just declaring a property will not assign it any value, so it will be zero. You have -didFinishLaunchingWithOptions in the application deletion set the alloc / init statement. Everything else looks good.

 KPBrowseExpensesNavigationController *nc = [[KPBrowseExpensesNavigationController alloc] init]; 

If you have a custom init, you can also use it, but just be sure to configure it before trying to use it.

+1
source

Source: https://habr.com/ru/post/918458/


All Articles