0

I want to pass a NSString value to a class method of a NSObject class: +(void)loadTableData:(myCompletion) compblock;. So I can only use a class variable NSString *categoryID for use in the class method. I declare it in the .h file trying to let it also be used for public access. But I tried many ways, and can only declare it like below. Other positions will give me warning.

#import <Foundation/Foundation.h>

typedef void(^myCompletion)(NSArray *);
NSString *categoryID;

@interface ThirdVCLoadTable : NSObject
+ (void)loadTableData:(myCompletion) compblock;
@end

Then the problem is I am still not able to access it in my view controller class. The error message is always: Property "categoryID" not found on object of type "ThirdVCLoadTable", no matter what I try in order to access the NSString *categoryID.

For example,

ThirdVCLoadTable *load = [[ThirdVCLoadTable alloc] init];
load.categoryID = [NSString stringWithFormat:@"%li", (long)indexPath.row + 1];
William Hu
  • 15,423
  • 11
  • 100
  • 121
Chenya Zhang
  • 463
  • 3
  • 11
  • 22

3 Answers3

0

It's not a property of the class. Try putting

@property (copy, nonatomic) NSString * categoryID;

below @interface

GetSwifty
  • 7,568
  • 1
  • 29
  • 46
  • But I cannot directly use it in a **class method** in the .m file if it is a property. – Chenya Zhang Oct 10 '16 at 05:21
  • That's not quite how you deal with class variable in Objective-C. See: http://stackoverflow.com/questions/695980/how-do-i-declare-class-level-properties-in-objective-c#696024 – GetSwifty Oct 10 '16 at 17:49
0

Declare a property in that class which you will call later from another view.

Suppose, in viewcontrollerA , in .h

@property (copy, nonatomic) NSString * StrName;

In viewcontrollerB, implement it like :

viewcontrollerA *vc = [[viewcontrollerA alloc]    initWithNib:@"viewcontrollerA" bundle:nil];
vc.StrName = @"XYZ";
[self pushViewController:vc animated:YES];

Get more help from this :

How to use NSString from NSObject class in multiple Classes (View Controllers)

Passing Data between View Controllers

Community
  • 1
  • 1
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
  • But I want to directly use the NSString in a **class method** in the NSObject's .m file as I described in the problem above. – Chenya Zhang Oct 10 '16 at 05:22
  • 1
    You have two separate class/viewcontroller, right ? edit the question with more info. Paste codes from both class. – Jamshed Alam Oct 10 '16 at 05:32
0

Step 1: suppose my ClassName is Info.

Info.h file

@interface Info : NSObject
typedef void(^myCompletion)(NSArray *);

@end
@interface ThirdVCLoadTable : NSObject

@property (strong, nonatomic) NSString * categoryID;

+ (void)loadTableData:(myCompletion) compblock;

Info.m file

+ (void)loadTableData:(myCompletion) compblock {

}

if you want to access Class Method in viewControllers than you can use like

ThirdVCLoadTable* load = [[ThirdVCLoadTable alloc] init];
load.categoryID = [NSString stringWithFormat:@"%li", (long)indexPath.row + 1];
Krutarth Patel
  • 3,407
  • 6
  • 27
  • 54