我的加载应用程序检查用户是否在文件系统中有文件,如果没有,它会使用 presentModalViewController 弹出一个 View Controller ,在用户使用该 View Controller 登录后,我的应用程序应该得到一些数据来自网络。
我的问题是,在 presentModalViewController 之后 View Controller 弹出,但是 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];
}
最佳答案
那是因为它就是这样工作的。 presentModalViewController:animated:
以模态方式呈现 View Controller ,然后返回。如果你想像这样拆分你的应用程序,那么你需要从呈现的 Controller 向父 Controller 发送信号,而不是它已经完成并且应该做你需要做的事情。
因此,一种方法是使用委托(delegate)模式。看看这个Apple doc获取有关如何准确执行您想要执行的操作的一些深入建议。
关于ios - PresentModalViewController 不会停止工作流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8887415/