2012-01-16 36 views
0

我的加載應用程序檢查用戶是否在文件系統中有一個文件,如果沒有,它會彈出一個使用presentModalViewController的視圖控制器,並且在用戶使用該視圖登錄後控制器我的應用程序應該從網上獲取一些數據。PresentModalViewController不會停止工作流程

我的問題是,在presentModalViewController之後,視圖控制器會彈出,但presentModalViewController之後的代碼繼續運行而不等待VC返回。

我的代碼:

- (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]; 
} 

回答

2

那是因爲它是如何工作。 presentModalViewController:animated:以模態方式呈現視圖控制器,然後返回。如果你想像這樣分割你的應用程序,那麼你需要從呈現的控制器回到父控制器,而不是完成它,並且應該做你需要做的事情。

所以一種方法是使用委託模式。請查看Apple doc,瞭解如何完成您想要做的事情的一些深入建議。

+0

真的嗎?我認爲模態的全部想法是在視圖完成之前停止一切。這讓我難過。 –

+0

這不是它的工作原理。主運行循環處理所有觸摸事件並繪製屏幕。如果它在呈現之後停止,則不會再進行觸摸事件或屏幕繪製,因此您甚至不會看到模式視圖控制器出現或能夠與其進行交互。 – mattjgalloway

+0

謝謝,我現在正在用文檔破解我的頭腦。 –