My application on load checks to see if a user has a file in the filesystem, and if it doesn’t it pops up a view controller using presentModalViewController and after the user logs in using that view controller my app is supposed to get some data from the web.
My problem is that after presentModalViewController the view controller does popup, but the code after presentModalViewController continues to run without waiting for the VC to return.
My code:
- (void)viewDidLoad
{
[self getPasswordFromFile];
responseData = [[NSMutableData data] retain];
NSString *URL= [NSString stringWithFormat:@"http://localhost/domything/get_status.php?ownerid=%@", nOwnerID];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[super viewDidLoad];
}
-(void)getPasswordFromFile
{
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingString:@"/pwfile"];
NSError *error;
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if(fileContents==nil)
{
// Show registration screen
LoginScreenVC *vcLoginScreen = [[[LoginScreenVC alloc] init] autorelease];
[vcLoginScreen setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vcLoginScreen animated:YES];
}
NSArray *arr = [fileContents componentsSeparatedByString:@":"];
// NSLog(@"ownerid is:%@, password is: %@", [arr objectAtIndex:0], [arr objectAtIndex:1]);
password = [arr objectAtIndex:1];
nOwnerID = [arr objectAtIndex:0];
[password retain];
[nOwnerID retain];
}
That’s because that’s how it works.
presentModalViewController:animated:presents the view controller, modally, then returns. If you want to split your app up like that then you’ll need to signal from the presented controller back to the parent controller than it’s finished and should do the stuff you need to do.So one way to do would be to use a delegate pattern. Take a look at this Apple doc for some in-depth advice on how to do exactly what you’re trying to do.