как позвонить в viewdid загрузить данные, когда действие события нажатия кнопки сделано - PullRequest
0 голосов
/ 14 ноября 2011

Я разрабатываю приложение на основе View.I, у меня есть метод, похожий на этот .- (IBAction) switchPage: (id) отправитель этот метод является действием события нажатия кнопки. Теперь, когда мы нажимаем на кнопку, она должна загружаться под видом, загружала данные, но она не загружается, может ли какая-нибудь помощь относительно этого

-(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 голосов
/ 14 ноября 2011

В качестве оптимизации 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];

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...