0

I am new to IOS. My first class is ViewController.I want to pass user_id from this class to snsViewController class. But i am getting null values when i am using @property.

I m writing this code in snsViewController.m

   ViewController *objViewController=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
   [objViewController jsonPost:getpostJson];

Plz suggest me some solution on this

I have written this code in ViewController.m

  -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
     respstring = [[NSString alloc]initWithData:loginJsonData  encoding:NSUTF8StringEncoding];
     NSLog(@"*******: %@",respstring);
     SBJsonParser *objSBJsonParser = [[SBJsonParser alloc]init];
      json = [[NSDictionary alloc]initWithDictionary:[objSBJsonParser   objectWithString:respstring]];
       NSLog(@"json: %@",json);
      /}
      //-(void)loginjonresponse
        // {

           NSNumber *error = [json objectForKey:@"error"];
              if([error intValue] == 0)
        {
              NSLog(@"u r connected with webserver ");
             }
           else
           {
               NSLog(@"connection fail");
     }

      NSString *value=[json objectForKey:@"value"];
        NSLog(@"value: %@",value);
           if (value ==(id)[NSNull null] || value==nil)
            {
             NSLog(@"login fail");

             }
        else
          {
             NSString *user_details=[[json objectForKey:@"value"]objectForKey:@"user"];
           // NSLog(@"user_deails%@",user_details);
            if ([user_details isEqual:@"false"])
             {
                 [self alertStatus:@"Please enter correct username and password" :@"Login Failed!"];
                    NSLog(@"incorrect user name password");

             }
           else
               {

                user_id=[[[json objectForKey:@"value"]objectForKey:@"user"]objectForKey:@"id"];
               NSLog(@"user_id: %@",user_id);
                firstname=[[[json  objectForKey:@"value"]objectForKey:@"user"]objectForKey:@"firstname"];
                NSLog(@"firstname: %@",firstname);
             lastname=[[[json objectForKey:@"value"]objectForKey:@"user"]objectForKey:@"lastname"];
                 NSLog(@"lastname: %@",lastname);
                 email=[[[json objectForKey:@"value"]objectForKey:@"user"]objectForKey:@"email"];
                NSLog(@"email: %@",email);
              currentroll_id=[[[json objectForKey:@"value"]objectForKey:@"user"]objectForKey:@"current_roll_id"];
               NSLog(@"current_roll_id: %@",currentroll_id);
                  currentcourse_id=[[[json objectForKey:@"value"]objectForKey:@"user"]objectForKey:@"current_course_id"];
                  NSLog(@"current_course_id: %@",currentcourse_id);

                  // [self classlist];



           #pragma After successful login move to sns page
              snsViewController *objsnsViewController=[[snsViewController alloc]initWithNibName:@"snsViewController" bundle:nil];


                 [self.view addSubview:objsnsViewController.view];

             }

           }

}

In this code i m getting user_id value.I want to pass this value to snsViewController.

Saba Sayed
  • 181
  • 2
  • 4
  • 11
  • You are using storyboards or simple xibs? – Vinay Jain Nov 28 '13 at 04:12
  • Show you other class code too – Tirth Nov 28 '13 at 04:12
  • I m using simple xibs. – Saba Sayed Nov 28 '13 at 04:17
  • Are you getting your values of user id's in an array? and you want to pass that array to secondviewcontroller when you go to secondview? – Manthan Nov 28 '13 at 04:18
  • if you google you will get lots of help https://www.google.co.in/search?q=pass+value+from+one+class+to+another+objective0C&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&channel=fflb&gws_rd=cr&ei=0MOWUpryHIPUrQempYDIDw#channel=fflb&q=pass+value+from+one+class+to+another+objective&rls=org.mozilla:en-US:official can do that? – Tirth Nov 28 '13 at 04:20
  • http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Aravind Bhuvanendran Nov 28 '13 at 04:20

2 Answers2

1

Take another instance variable in snsViewController.h file and make property of that. For example

.h file
NSString *userID;
@property (nonatomic,retain) NSString *userID;

.m file
@synthesize userID

Then in this piece of code

#pragma After successful login move to sns page
              snsViewController *objsnsViewController=[[snsViewController alloc]initWithNibName:@"snsViewController" bundle:nil];

objsnsViewController.userID = user_id;
                 [self.view addSubview:objsnsViewController.view];
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
1
There are many ways you can pass the data from one class to another.
For example lets say one is your model class and two other class is your 
viewController class.

Now this is your model class below in which you need to declare the accessor method and designated initializer:-

@interface yourmodel : NSObject
@property (nonatomic, strong)NSString *title;
-(id)initWithTitle:(NSString*)title

@implementation yourmodel
-(id)initWithTitle:(NSString*)title
{
    self=[super init];
       if (self)
       {
           self.title=title;
        }
        return self;
}
@end

This one is your firstViewController class in which you will send data to second view controller:-

@class yourmodel
@interface yourFirstViewController : UIViewController
@property (nonatomic,strong)yourmodel *dataModel;
@property (nonatomic,strong)NSMutableArray *yourArray;

 @implementation yourFirstViewController
- (void)viewDidLoad
{
yourmodel *model=[[yourmodel alloc] initWithTitle:@"yourString"];
[yourArray addObject:model];
}

//Assuming this is your tableview delegate method of first view controller in which you are going to send the data to second view controller

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        yourSecondViewController *secondcontroller = [[yourSecondViewController alloc] initWithNibName:
@"yourSecondViewController" bundle:nil];
    secondcontroller.dataModel=[self.yourArray objectAtIndex:indexPath.row];
}
@end

Now this is you secondView Controller in which you want the data from first view controller:-

 @class yourmodel
    @interface yourSecondViewController : UIViewController
    @property (nonatomic,strong)yourmodel *dataModel;

     @implementation yourSecondViewController
    - (void)viewDidLoad
    {
     NSLog(@"dataModel.title=%@",self.dataModel.title);
    }

Note :- In each view controller you need to decalare the reference of model class for receiving the data.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56