IOS:如何在IOS中将值从一个类传递到另一个类

IOS:如何在IOS中将值从一个类传递到另一个类,ios,objective-c,Ios,Objective C,我是IOS新手。我的第一个类是ViewController。我想将用户id从此类传递给snsViewController类。但是当我使用@property时,我得到的是空值 我正在snsViewController.m中编写此代码 ViewController *objViewController=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; [objViewController jsonP

我是IOS新手。我的第一个类是ViewController。我想将用户id从此类传递给snsViewController类。但是当我使用@property时,我得到的是空值

我正在snsViewController.m中编写此代码

   ViewController *objViewController=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
   [objViewController jsonPost:getpostJson];
  -(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];

             }

           }

}
请给我一些解决方案

我已经在ViewController.m中编写了这段代码

   ViewController *objViewController=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
   [objViewController jsonPost:getpostJson];
  -(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];

             }

           }

}

在这段代码中,我得到了user_id值。我想把这个值传递给snsViewController。

在snsViewController.h文件中取另一个实例变量,并将其作为属性。比如说

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

.m file
@synthesize userID
然后在这段代码中

#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];
下面是您的模型类,您需要在其中声明访问器方法和指定的初始值设定项:-

@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
这是您的第一个ViewController类,您将在其中向第二个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];
}
 @class yourmodel
    @interface yourSecondViewController : UIViewController
    @property (nonatomic,strong)yourmodel *dataModel;

     @implementation yourSecondViewController
    - (void)viewDidLoad
    {
     NSLog(@"dataModel.title=%@",self.dataModel.title);
    }
//假设这是第一个视图控制器的tableview委托方法 您将在其中向第二视图控制器发送数据

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        yourSecondViewController *secondcontroller = [[yourSecondViewController alloc] initWithNibName:
@"yourSecondViewController" bundle:nil];
    secondcontroller.dataModel=[self.yourArray objectAtIndex:indexPath.row];
}
@end
现在,这是您希望从第一个视图控制器获取数据的第二个视图控制器:-

@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];
}
 @class yourmodel
    @interface yourSecondViewController : UIViewController
    @property (nonatomic,strong)yourmodel *dataModel;

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

注意:-在每个视图控制器中,您需要标记模型类的引用以接收数据。

您正在使用故事板或简单XIB?显示其他类代码以使用简单XIB。您是否正在数组中获取用户id的值?当你转到secondview时,你想将该数组传递给secondviewcontroller吗?如果你使用谷歌搜索,你会得到很多帮助。你可以做到这一点吗?它无法识别用户ID。我发现错误“在类型为“snsViewController”的对象上找不到属性“userID”。你需要在snsViewController的头文件中创建实例变量