Search code examples
iphoneobjective-ccocoa-touchuiapplicationdelegate

placing string from one view controller to the delegate


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); }

Solution

  • 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;