简体   繁体   English

如何从视图控制器访问NSObject类/实例变量?

[英]How to access NSObject class/instance variable from view controllers?

I want to pass a NSString value to a class method of a NSObject class: +(void)loadTableData:(myCompletion) compblock; 我想将NSString值传递给NSObject类的类方法: +(void)loadTableData:(myCompletion) compblock; . So I can only use a class variable NSString *categoryID for use in the class method. 因此,我只能在类方法中使用类变量NSString *categoryID I declare it in the .h file trying to let it also be used for public access. 我在.h文件中声明了它,试图将其也用于公共访问。 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 . 错误消息始终为: 无论如何尝试访问NSString *categoryID ,都无法在类型“ ThirdVCLoadTable”的对象上找到属性“ categoryID”

For example, 例如,

ThirdVCLoadTable *load = [[ThirdVCLoadTable alloc] init];
load.categoryID = [NSString stringWithFormat:@"%li", (long)indexPath.row + 1];

It's not a property of the class. 这不是该类的属性。 Try putting 试穿

@property (copy, nonatomic) NSString * categoryID;

below @interface 在@interface下面

Declare a property in that class which you will call later from another view. 在该类中声明一个属性,您稍后将在另一个视图中调用该属性。

Suppose, in viewcontrollerA , in .h 假设在viewcontrollerA中,在.h中

@property (copy, nonatomic) NSString * StrName; @属性(副本,非原子)NSString * StrName;

In viewcontrollerB, implement it like : 在viewcontrollerB中,将其实现为:

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) 如何在多个类中使用NSObject类中的NSString(视图控制器)

Passing Data between View Controllers 在视图控制器之间传递数据

Step 1: suppose my ClassName is Info. 步骤1:假设我的ClassName是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 Info.m文件

+ (void)loadTableData:(myCompletion) compblock {

}

if you want to access Class Method in viewControllers than you can use like 如果您想在viewControllers中访问Class方法,则可以像

ThirdVCLoadTable* load = [[ThirdVCLoadTable alloc] init];
load.categoryID = [NSString stringWithFormat:@"%li", (long)indexPath.row + 1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM