关于C#:IOS:如何在ios中将值从一个类传递到另一个类

IOS:How to pass a value from one class to another class in ios

本问题已经有最佳答案,请猛点这里访问。

我刚接触过iOS。我的第一个类是viewcontroller。我想将用户ID从这个类传递到snsviewcontroller类。但是当我使用@property时,得到的是空值。

我在snsviewcontroller.m中编写此代码。

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

请给我一些解决办法

我在viewcontroller.m中编写了这个代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  -(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];

             }

           }

}

在这个代码中,我得到用户的ID值。我想把这个值传递给SNSVIEW控制器。


1
2
3
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.

下面是您的模型类,您需要在其中声明访问器方法和指定的初始值设定项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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类,您将在其中向第二个视图控制器发送数据:

1
2
3
4
5
6
7
8
9
10
11
@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];
}

/假设这是您的第一视图控制器的Tabview视图委托方法在其中,您将把数据发送到第二视图控制器。

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

现在,这是您要从第一个视图控制器获取数据的第二个视图控制器:

1
2
3
4
5
6
7
8
9
 @class yourmodel
    @interface yourSecondViewController : UIViewController
    @property (nonatomic,strong)yourmodel *dataModel;

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

注意:在每个视图控制器中,您需要为接收数据删除模型类的引用。


以snsviewcontroller.h文件中的另一个实例变量为例,并将其设置为属性。例如

1
2
3
4
5
6
.h file
NSString *userID;
@property (nonatomic,retain) NSString *userID;

.m file
@synthesize userID

那么在这段代码里

1
2
3
4
5
#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];