简体   繁体   中英

How to delay a method?

I am working on an iPhone application. When the application starts I authenticate, establish a Restful connection, get data in form of XML file, parse it and then display it in form of table view. Here is my problem. If the user information is already stored on the application or if it is hard-coded in the code, the application takes the authentication user name and password and proceeds to display data in table format. I was till now testing with hard-coded data. Now I want to display another view which asks for user name and password if it is not present in the application..ie I want to make the user name and password dynamic as opposed to static. In my case when the use is busy inputting the user name and password, the "viewDidLoad" method tries to establish a connection with the server with blank user name and password failing the server connection and the table loading. But if I run the application the second time, the application finds the user name and password entered previously and loads the tables fine.

Is there a way I can delay the process of server connection if the application does not find the user name and password? How can I signal the server connection method after the user has entered his user name and password?

Thanks for your help.

Can you post some of your code? It's difficult to speculate without seeing what's going on but I'll give it a try anyway.

You need to move the code where you connect to the server out of viewDidLoad and put it in a callback method or something. I would suggest throwing an alert with a name and password field, make your view controller the alert's delegate and then have the clickedButtonAtIndex function connect to the server.

Example:

-(void)viewDidLoad {
    UIAlertView *loginAlert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Please enter your username and password below" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login",nil];

    //You might have to do some fiddling with the frame sizes to make it fit in the alert.
    UITextField *userNameField = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, lengthX, lengthY)];
    userNameField.placeholder = @"User Name";
    [loginAlert addSubview:userNameField];
    //loginAlert will retain this so we release it now.
    [userNameField release];



    [UITextField *passwordNameField = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, lengthX, lengthY)];
    passwordNameField.secureTextEntry = YES;
    [loginAlert addSubview:passwordNameField];
    [passwordNameField release]

    //This line tells the iPhone to stretch out your AlertView so it's big enough for your two textViews, we use 0 and 130 respectively but we only have one text field.
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(stretchX, stretchY);

    [loginAlert setTransform:myTransform];
    [loginAlert show];
    [loginAlert release];
}


//Implement this method, it will be called when the user clicks the "login" button on your alert.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //Usually if they click cancel you do nothing, thats buttonIndex 0
        if(buttonIndex == 1) {
        //Connect to the server with the correct login and password.
    }
}

Once you display a view in a window ViewDidLoad will ALWAYS be called, thats why its called viewDidLoad...What you need to do, is create a diffrent view for the login steps, then when the user is done loging in they will click a login button which will push the view that connects to your restful service. If the user data is already present, then skip the login page all together and go straight to your table view view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM