将字符串从一个视图控制器放置到委托中

发布于 2024-11-15 22:41:13 字数 1593 浏览 1 评论 0原文

从我的视图控制器中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风透绣罗衣 2024-11-22 22:41:13

profileViewController 是你的类。要直接访问 userId,它应该被定义为 Edit

+ (NSString *) userId;

而不是

- (NSString *) userId; // (or)
@property (nonatomic, copy) NSString * userId;

Edit

因为 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;

profileViewController is your class. To access userId directly, it should be defined as,

+ (NSString *) userId;

and not,

- (NSString *) userId; // (or)
@property (nonatomic, copy) NSString * userId;

Edit

Since profileViewController is one of the view controllers in the tab bar controller instance rootController, you can try getting the profileViewController instance like this,

profileViewController * theController;
NSArray * viewControllers = rootController.viewControllers;
for ( UIViewController * viewController in viewControllers ) {
    if ( [viewController isMemberOfClass:[profileViewController class]] ) {
        theController = viewController;
    }
}

NSString * userID = theController.userId;
有木有妳兜一样 2024-11-22 22:41:13

您需要将其设置为实例变量。
如果您有 -(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文