2013-11-28 85 views
0

我是IOS新手。我的第一个类是ViewController.I想要将此类的user_id传递给snsViewController类。但是当我使用@property时,我得到空值。IOS:如何将一个类的值传递给ios中的另一个类

我正在写在snsViewController.m这个代码

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

PLZ建议我在此

一些解决方案,我已经在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]; 

      } 

      } 

} 

写了这个代码在这个代码即时通讯获取user_id值。我想将此值传递给snsViewController。

+0

您正在使用故事板或简单的xib? –

+0

也向你展示其他课程代码 – Tirth

+0

我使用简单的xibs。 –

回答

1

在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]; 
+0

它不认识userID。我得到错误“属性'userID'没有找到对象的类型'snsViewController' –

+0

你需要在snsViewController的头文件中创建实例变量 –

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. 

现在,这是低于模型类中,你需要声明的访问方法和指定初始化: -

@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 

这是您的firstViewController类,您将在其中将数据发送到第二个视图控制器: -

@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]; 
} 

//假设这是第一个视图控制器 中你要发送数据到第二视图控制器你的tableview委托方法

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

现在这是你secondView控制器在其中想从第一视图控制器中的数据: -

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

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

注: - 在每个视图控制器需要decalare用于接收第模型类的基准e数据。