从我的视图控制器中的NSString获取值到我的委托时遇到问题。问题是userId字符串。它在委托中出现错误
错误:访问未知的'userId'类方法
查看控制器代码段(profileViewController.m)
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
NSDictionary *rowData = [self.userArray objectAtIndex:0];
self.userId = [[NSMutableString alloc] initWithFormat:@"%@", [rowData objectForKey:@"id"]];
NSLog(@"DONE PARSING DOCUMENT");
NSLog(self.userId);
}
- (void)viewDidLoad
{
self.title = @"Profile";
interestingTags = [[NSSet alloc] initWithObjects: INTERESTING_TAG_NAMES];
self.userArray = [[NSMutableArray alloc] init];
[super viewDidLoad];
}
委托
#import "profileViewController.h"
@implementation experimentAppDelegate
@synthesize window;
@synthesize rootController;
-(void)goingOffline
{
NSMutableData *data = [NSMutableData data];
NSString *userID = profileViewController.userId;
NSString *userString = [[NSString alloc] initWithFormat:@"id=%@", userID];
//NSLog(nameString);
//NSLog(numberString);
[data appendData:[userString dataUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:@"http://www.website.net/test.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData); }
答案 0 :(得分:1)
profileViewController
是你的班级。要直接访问userId
,应将其定义为
+ (NSString *) userId;
而不是,
- (NSString *) userId; // (or)
@property (nonatomic, copy) NSString * userId;
修改强>
由于profileViewController
是标签栏控制器实例rootController
中的一个视图控制器,您可以尝试获取此类profileViewController
实例,
profileViewController * theController;
NSArray * viewControllers = rootController.viewControllers;
for ( UIViewController * viewController in viewControllers ) {
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
theController = viewController;
}
}
NSString * userID = theController.userId;
答案 1 :(得分:0)
您需要将其设为实例变量。
如果您有-(NSString *)
,即使您将其导入另一个文件,也只能在该文件中访问它。如果您使用+(NSString *)
,则不会再出现该错误。同样适用于空洞+(void)
。祝你好运