hengecyche
hengecyche

Reputation: 409

Cannot access property from class

//MigrationVC.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MigrationVC : UIViewController

@end

//MigrationVC.m
#import "MigrationVC.h"

@interface MigrationVC()
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIProgressView *progressView;

@end

@implementation MigrationVC

@end

//CoreData
#import "CoreData.h"
#import "MigrationVC.h"

@interface CoreData()
@property (nonatomic,retain) MigrationVC *migrationVC;
@end

-(void)obsererForKeyPath:(NSString*)keyPath object:(id)object change:(NSDictionary*)change context:(void*)context
{
    if([keyPath isEqualToString:@"migrationProgress"])
    {
        dispatch_async(dispatch_get_main_queue(),^{
            float progress=[[change objectForKey:NSKeyValueChangeNewKey] floatValue];
            self.migrationVC.progress=progress;
        });
    }
}

I am trying to learn CoreData and migration right now but this is giving me a quite a headache.

I am trying to access the outlet properties from another classes but always gives red warning (Property 'label' not found on object of type MigrationVC*).

I tried adding a NSString property in .h file which was accessible but when i tried to change the outlet from .m to .h file i couldn't ctrl+drag the view in the .h file.

I never had this problem. I have accessed outlet from .m file many times in the past but it just gives me warning now. How can i access the properties while outlet in .m file?

The problem is i can't add the outlet in .h file

I cannot outlet the properties in .h file.

Upvotes: 4

Views: 939

Answers (4)

T Bone
T Bone

Reputation: 15

In CoreData.m your MigrationVC is set as retain. I don't know if you can use it since with ARC.

@property (nonatomic,retain) MigrationVC *migrationVC;

It should be

@property (nonatomic,strong) MigrationVC *migrationVC;

And in MigrationVC your outlets should be weak not strong and in .h to be accesible from CoreData.m

Upvotes: 1

Moonkid
Moonkid

Reputation: 916

You properties are declared in the private category so they are not visible for other classes. Only properties declared in a header file are visible.

Upvotes: 1

Duncan C
Duncan C

Reputation: 131398

As others have pointed out, you need to declare your properties in your header file if you want them to be accessible from other classes. You can, and you should. If you think you can't do that, explain why.

More important, though, is that you should not do what you are trying to do. You should not try to access a view controller's view objects from outside the view controller. That violates the principle of encapsulation, an important principle of object-oriented design. (It means that code outside of your view controller is dependent on the appearance of your view controller. If you later decide to make internal changes to your view controller, you are very likely to break outside code.) In addition to the somewhat abstract "It's bad design" reason for not doing it, it often doesn't work, because at the time to try to modify a view controller's views, they don't exist yet.

Instead, you should create DATA properties (like strings, or floating point progress values) in your view controller and expose those. Then have your view controller's viewWillAppear method install the data into it's views as appropriate. That way the data properties become part of the view controller's public contract without being tied to the internal details of the view controller.

Upvotes: 3

fnc12
fnc12

Reputation: 2237

You have to transfer you outlet properties from .m file to .h file (copy and paste). If you want your properties to be public so they have to be declared in header file. If you want them to be private - declare them in implementation file.

//MigrationVC.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MigrationVC : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIProgressView *progressView;
@end

//MigrationVC.m
#import "MigrationVC.h"

@interface MigrationVC()

@end

@implementation MigrationVC

@end

Upvotes: 4

Related Questions