【问题标题】:PresentModalViewController doesn't stop the workflowPresentModalViewController 不会停止工作流
【发布时间】:2012-01-16 22:51:39
【问题描述】:

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

【问题讨论】:

    标签: ios xcode modalviewcontroller


    【解决方案1】:

    那是因为它是这样工作的。 presentModalViewController:animated: 以模态方式呈现视图控制器,然后返回。如果你想像那样拆分你的应用程序,那么你需要从呈现的控制器向父控制器发出信号,而不是它已经完成并且应该做你需要做的事情。

    因此,一种方法是使用委托模式。看看这个Apple doc 以获得一些关于如何做你想做的事情的深入建议。

    【讨论】:

    • 真的吗?我认为模态的整个想法是停止一切,直到视图完成。这让我很难过。
    • 根本不是这样的。主运行循环处理所有触摸事件并绘制屏幕。如果它在呈现后停止,则不会再进行触摸事件或屏幕绘制,因此您甚至不会看到模态视图控制器出现或无法与之交互。
    • 没问题。是的,我建议给我链接的文档好好阅读。他们解释了做你想做的事,这将是一次很好的学习体验。
    • 我不明白的一件事:我将当前视图配置为模态的委托,但模态如何知道要调用哪个函数?
    • 您将制定自己的协议。然后,您将 presentViewController:animated: 的调用者设置为您呈现的视图控制器的委托。然后当模态完成时,调用定义的委托方法。
    猜你喜欢
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多