开发者

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]开发者_如何学Python 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); }


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;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜