【发布时间】:2013-09-07 04:57:55
【问题描述】:
我有带有用户名和密码的登录页面。当我输入正确的凭据时,它将转到另一个视图控制器。在这个视图控制器中,我有按钮来单击匹配的用户信息。如果用户名匹配,我只需要检索他们的数据并显示。
代码:
viewController.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" 从viewController 传递到ViewController1.m。 CategoryNames & categoryIds 包含特定用户产品名称和 ID 的数组。如果ObjForKey=@"id" 匹配,我需要获取该用户categoryNames & categoryIds
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];
}];
【问题讨论】:
-
这适用于多个用户。每个用户在我的服务器中注册他们的名字。注册后,我的服务器将为每个用户生成一个用户 ID。然后每个用户写一些东西。这存储在每个用户中。如果用户安装应用程序并输入用户名、密码,它将转到 viewcontroller1.m 。在具有按钮的 viewcontroller1.m 中,如果单击按钮,我需要从服务器检索特定的用户数据。
-
@Meda:你明白了吗?
-
您是在问如何将 id 从一个视图传递到另一个视图?
-
你是对的。但是我怎样才能将这个 ID 传递给 sql 查询呢? const char *sql = "SELECT id,cat_name FROM categories where company_id = ObjForKey=@"id";
标签: iphone arrays json sqlite login