user1833848
user1833848

Reputation: 21

Can't use method from class in other file

I am not able to use one of my methods that i implemented in my tableviewcell file in my tableview controller implementation. I tried searching the web and xcode help with no luck. My codes looks like this:

TableViewController.h:

    #import TableViewCell.h

    @interface TableViewController : UITableViewController


    @property (nonatomic, strong) IBOutlet UIBarButtonItem *A1Buy;
    @property (nonatomic, getter = isUserInteractionEnabled) BOOL userInteractionEnabled;

    - (IBAction)A1Buy:(UIBarButtonItem *)sender;

TableViewController.m:

    @implementation A1ViewController

    @synthesize A1Buy = _A1Buy;
    @synthesize userInteractionEnabled;

    - (IBAction)A1Buy:(UIBarButtonItem *)sender {
   [TableViewCell Enable]; //this is where it gives an error

    }

TableViewCell.h:

    @interface TableViewCell : UITableViewCell {
    BOOL Enable;
    BOOL Disable;
    }
    @property (nonatomic, getter = isUserInteractionEnabled) BOOL userInteractionEnabled;

TableViewCell.m:

    @implementation TableViewCell;

    @synthesize userInteractionEnabled;

    - (BOOL) Enable {
    return userInteractionEnabled = YES;
    }
    - (BOOL) Disable {
    return userInteractionEnabled = NO;
    }

As you can see i am trying to enable user interaction with a button, but Xcode only gives me errors like "class does not have this method" and stuff like that. All files are importet correctly so thats not why. Would appreciate any help. Thanks!

Upvotes: 2

Views: 105

Answers (2)

Midhun MP
Midhun MP

Reputation: 107231

You are declared the - (BOOL) Enable as an instance method. You cannot call the instance method using Class name. Solutions:

  1. Declare the method as Class method

     + (BOOL) Enable
    
  2. Create an object of the class then call the method using that object

     TableViewCell *cellObj = [[TableViewCell alloc] init];
     [cellObj Enable];
    

Please read more about class method here.

Please refer the ios coding conventions here.

Upvotes: 0

jrturton
jrturton

Reputation: 119292

First, name your methods and variables according to Cocoa standards - classes have a capitalised first letter, variables and methods have a lower case first letter.

Doing that should make it obvious that you're calling the Enable method on the TableViewCell class, where it is actually an instance method. You need to get a pointer to a specific table view cell and call the method on that.

Also, your methods as implemented are very confusing. Why are they returning the result of an assignment as a Boolean? This will always return YES. You may need to study some basic objective-c training resources.

Upvotes: 1

Related Questions