将字符串从一个视图控制器放置到委托中
从我的视图控制器中的 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); }
having issues getting a value from a NSString in my view controller to my delegate. The issue is the userId string. it comes back with an error in the delegate
error: accessing unknown 'userId' class method
view controller snippet ( 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];
}
delegate
#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); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
profileViewController
是你的类。要直接访问userId
,它应该被定义为 Edit,而不是
Edit
因为
profileViewController
是选项卡栏控制器实例中的视图控制器之一rootController
,您可以尝试像这样获取profileViewController
实例,profileViewController
is your class. To accessuserId
directly, it should be defined as,and not,
Edit
Since
profileViewController
is one of the view controllers in the tab bar controller instancerootController
, you can try getting theprofileViewController
instance like this,您需要将其设置为实例变量。
如果您有
-(NSString *)
它只能在该文件中访问,即使您将其导入到另一个文件中也是如此。如果您使用+(NSString *)
您将不会再收到该错误。对于 void+(void)
也是如此。祝你好运You need to make it instance variable.
If you have
-(NSString *)
it can only be accessed within that file even if your importing it in another file. If you use+(NSString *)
you will not get that error anymore. The same works for voids+(void)
. Good Luck