Ryan Cohen
Ryan Cohen

Reputation: 13

How can I use an IBOutlet from another Class file into a different .m file?

MyGems.h

#import <UIKit/UIKit.h>


@interface MyGems : UIViewController {

    IBOutlet UIImageView *lock1;

Collect.m

#import "Collect.h"
#import "MyGems.h"

@implementation Collect

- (IBAction) unlock {

    if (lock1.hidden = NO) {

        ruby.hidden = NO;
    }

Upvotes: 0

Views: 549

Answers (2)

kgutteridge
kgutteridge

Reputation: 8981

You need to make sure you have a reference to lock1 in your Collect class, most likely you will want to achieve this by having a reference to the Collect class in MyGems and then moving this view logic back into MyGems rather than in your Collect model class

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299455

You should almost never do this. The view controller (MyGems, which should be named something like MyGemsViewController) is responsible for managing this view. Other classes should not reach in and modify its IBOutlets. Doing so will cause you significant problems when the view managed by MyGems unloads and lock1 surprisingly becomes nil.

It's unclear what Collect is in this case; I assume it is another view controller? I'm not clear why unlock isn't a method on MyGemsViewController.

Also note that this code is incorrect, and should be throwing warnings at you telling you it's incorrect:

if (lock1.hidden = NO) {

This assigns NO to lock1.hidden. You meant to use ==, but you should never test against NO this way. You should do it this way:

if (! lock1.hidden) {

You must be careful of testing booleans against YES and NO. There are many true values that do not equal YES.

Upvotes: 1

Related Questions