-1

我有带有用户名和密码的登录页面。当我输入正确的凭据时,它将转到另一个视图控制器。在这个视图控制器中,我有按钮来单击匹配的用户信息。如果用户名匹配,我只需要检索他们的数据并显示。

代码:

视图控制器.m:

if ([responseData length]){

    NSLog(@"Response ==> %@", responseData);
    NSMutableDictionary *dict=[responseData JSONValue];                
    NSMutableDictionary *dict1=[responseData JSONValue];                
    NSInteger success = [(NSNumber *) [dict objectForKey:@"success"] integerValue];
    NSInteger id1 = [(NSNumber *) [dict1 objectForKey:@"id"] integerValue];               

   NSLog(@"%d",success);
    if(success == 1){
        NSLog(@"Login SUCCESS");
        [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
        [self.navigationController pushViewController:overlay animated:YES];

    } else {
        NSString *error_msg = (NSString *) [dict objectForKey:@"error_message"];
        [self alertStatus:error_msg :@"Login Failed!"];
    }
}

上面的代码objForKey=@"success"包含username & password. ObjForKey=@"id"包含每个用户 ID。如果此 id 匹配,我需要显示该 id 数据。

viewController1.m:

在这里我需要得到ObjForKey=@"id"。company_id 存储ObjForKey=@"id"数据。如何在选择查询中传递ObjForKey=@"id"from viewControllerto 。&包含特定用户产品名称和 ID 的数组。如果匹配,我需要获取该用户&ViewController1.mCategoryNamescategoryIdsObjForKey=@"id"categoryNamescategoryIds

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

    const char *sql = "SELECT id,cat_name FROM categories where company_id = ObjForKey=@"id";
    NSLog(@"sql is %s",sql);

    categoryNames = [NSMutableArray array];
    categoryIds = [NSMutableArray array];
    sqlite3_stmt *statement;
    // int catID = 0;
  if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

    // We "step" through the results - once for each row.
    while (sqlite3_step(statement) == SQLITE_ROW) {
        NSString *categoryName = [[NSString alloc] 
                                  initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
        NSLog(@"catName is %@",categoryName);
        NSInteger categoryId = sqlite3_column_int(statement, 0);

        [categoryNames addObject:categoryName];
        [categoryIds addObject:@(categoryId)];
    } 
  }
}

这里 UIActionSheet 显示特定用户 ID 的 categoryNames:

TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"Design"];       


 for (int i = 0; i<[categoryNames count]; i++ ) {           

            NSLog(@"catearray is %@",categoryNames);

            [actionSheet addButtonWithTitle:[categoryNames objectAtIndex:i] block:^{

                [self actionSheetClickedButtonAtIndex:i];

        }];
4

1 回答 1

1

您可以NSUserDefaults为此使用:

if ([responseData length]){

    NSLog(@"Response ==> %@", responseData);
    NSMutableDictionary *dict=[responseData JSONValue];                                
    NSInteger success = [(NSNumber *) [dict objectForKey:@"success"] integerValue];

    NSLog(@"%d",success);
    if(success == 1){
        //save the ID
        NSInteger id1 = [(NSNumber *) [dict objectForKey:@"id"] integerValue];
        NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
        [userData setInteger:id1 forKey:@"id"];
        [userData synchronize];       
        NSLog(@"Login SUCCESS");
        [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
        [self.navigationController pushViewController:overlay animated:YES];

    } else {
        NSString *error_msg = (NSString *) [dict objectForKey:@"error_message"];
        [self alertStatus:error_msg :@"Login Failed!"];
    }
}

然后要取回id值,您可以在 ViewController1.m 中执行此操作:

//Get the Id
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
NSInteger id1 = [userData integerForKey:@"id"];

编辑:

但是我怎样才能将这个 ID 传递给 sql 查询呢?

不知道你为什么objForKey在你的查询中试试这个:

NSString *sql  = [NSString stringWithFormat:
                    @"SELECT id,cat_name FROM categories where company_id = %d", id1];
于 2013-09-08T21:14:06.207 回答