当按钮点击事件动作完成时,如何在视图下调用确实加载数据

时间:2011-11-14 09:59:07

标签: objective-c xcode cocoa-touch

我正在开发基于View的应用程序。我的方法看起来像这样.-(IBAction)switchPage :( id)sender这个方法是按钮点击事件动作。现在,当我们点击按钮时,它应该在视图下加载加载数据,但它没有加载可以任何一个关于此

的帮助
-(IBAction)switchPage:(id)sender

    {
    if(self.viewTwoController == nil)
    {
        ViewTwoController *viewTwo = [[ViewTwoController alloc]
                                      initWithNibName:@"View2" bundle:[NSBundle mainBundle]];
        self.viewTwoController = viewTwo;
        [viewTwo release];
    }
  [self.navigationController pushViewController:self.viewTwoController animated:YES];

[connection release];
}
//in viw did load i have this code 

- (void)viewDidLoad {
    [super viewDidLoad];

    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://108.16.210.28/Account/LogOn"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//  label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    NSString *post =[[NSString alloc] initWithFormat:@"usernameField=%@&passwordField=%@",usernameField.text,passwordField.text];
    NSURL *url=[NSURL URLWithString:@"https://108.16.210.28/SSLLogin/Account/LogOn"];

    NSLog(post);
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    /* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects
     */

    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);
}

在这里输入代码

1 个答案:

答案 0 :(得分:0)

作为优化,NSViewControllers直到最后一刻才加载他们的视图,即实际访问视图时。因此,要立即加载视图,请在初始化后调用[viewTwoController view]并加载nib,然后调用viewDidLoad方法。

编辑:您的代码如下所示:

-(IBAction)switchPage:(id)sender {
    if(self.viewTwoController == nil) {

        ViewTwoController *viewTwo = [[ViewTwoController alloc]
            initWithNibName:@"View2" bundle:[NSBundle mainBundle]];
        self.viewTwoController = viewTwo;
        [viewTwo release];
        [viewTwo view]    // calls loadView, loads the nib then calls viewDidLoad
}
[self.navigationController pushViewController:self.viewTwoController animated:YES];
[connection release];

}