ios - PresentModalViewController 不会停止工作流

标签 ios xcode modalviewcontroller

我的加载应用程序检查用户是否在文件系统中有文件,如果没有,它会使用 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/

相关文章:

ios - 冗余计算减慢 UITableView 速度

ios - 核心数据 : table view FRC number of fetched objects is zero

ios - 不显示仅包含数字的iOS推送通知

ios - Xcode 4.3.1 安装失败

swift - 在模态 UIViewController 解除后 View 生命周期中更新 Controller 的位置

ios - 如何制作/编程 UIlabel 以根据用户输入水平滚动?

ios - 仅记录我在 Xcode 中打印的内容

iphone - 安装新 Leopard 后重新安装 iPhone 代码签名证书

iphone - ios6 中解雇 modalViewcontroller 错误

ios - 如何计算 iPad 上模态视图的底部偏移量?