-3

我在这里有我的按钮,在这个功能结束时我想切换到另一个视图控制器。但是我不知道如何切换它,我只为应用程序的 iphone 版本工作。

- (IBAction)loginMe:(UIButton *)sender {
    [self.view endEditing:YES];
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.111:8888/api.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *requester = [NSString stringWithFormat:@"type=account&username=%@&password=%@",self.username.text,self.password.text];
    NSData *requestBody = [requester dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:requestBody];
    NSURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    if([responseString isEqualToString:@"1"])
    {
        self.data.text = @"Username or password incorrect";
    }
    else
    {
        NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *jsonParsingError = nil;
        NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:data
                                                                  options:0 error:&jsonParsingError];

        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

        [ud setInteger:1 forKey:@"isLoggedin"];
        [ud setObject:[publicTimeline objectAtIndex:0] forKey:@"userid"];
        [ud setObject:[publicTimeline objectAtIndex:1] forKey:@"username"];
        [ud setObject:[publicTimeline objectAtIndex:2] forKey:@"loginstr"];
        [ud synchronize];
        [self animateLoginBox];
        self.data.text = responseString;
    }
}

任何帮助将非常感激!

谢谢!

4

1 回答 1

0

您的视图控制器是否嵌入在导航控制器中?如果是这样;

将该视图控制器 .h 导入当前视图控制器的顶部。然后在情节提要中为该视图控制器创建情节提要 ID。然后在移动到新视图控制器的方法中试试这个:

 YourNewViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:@"yourStoryboardID"];
[self.navigationController pushViewController:newVC animated:YES];

但是如果它没有嵌入到导航控制器中,试试这个:

YourNewViewController *newVC = [[YourNewViewController alloc]init];
[self presentModalViewController:newVC animated:YES];

希望这可以帮助!

于 2013-07-27T02:31:52.220 回答