【问题标题】:Modal view won't dismiss模态视图不会关闭
【发布时间】:2013-02-17 12:24:28
【问题描述】:

我发现了许多类似的问题,但无论如何都找不到我的问题的解决方案。 所以,我的应用程序(它由 tabView 组成)需要在第一次启动后从服务器下载数据。为此,它调用模态视图控制器,在其中使用 Reachability.h/m 和 NSNotification defaultCenter 检查互联网连接。如果可以访问互联网,则开始下载并保存数据。完成后,我想关闭模态打开的视图控制器,但它一遍又一遍地重新加载。我坚持了将近2天,请帮忙:)

这是我的代码的一部分:在第一个 ViewController 中

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //checking for data if not calls modalViewController
    if(firstStart){
        [self performSegueWithIdentifier:@"startDownload" sender:self];
    }

}

在第二个(模态视图控制器)

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

     internetReachable = [Reachability reachabilityForInternetConnection];
     [internetReachable startNotifier];

     // check if a pathway to a random host exists
     hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"];
     [hostReachable startNotifier];  

}

在 checkNetworkStatus 中完成检查互联网连接是否存在互联网连接执行 JSON 下载和解析,最后调用 Close 方法

-(void)Close
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

在这个调用之后,屏幕转到第一个 ViewController 并立即变为第二个,所有的东西都再次执行,它有点卡在循环中。

【问题讨论】:

  • 可能 firststart 总是正确的。如何设置 firststart?
  • 你说的 firststart 是什么意思?
  • 我制作了 sipler 项目只是为了测试。 2 viewcontorllers 1-st 在 vi​​ewdidappear 中通过 segue 调用第二个。 2-nd 从他的 viewdidappear 中关闭 [self dismissViewControllerAnimated:YES completion:nil]; - 我的情况完全一样,

标签: iphone ios modalviewcontroller


【解决方案1】:

问题是第一个视图控制器的viewDidAppear 不仅在它第一次出现时被调用,而且在第二个视图控制器的模态被关闭并且第一个视图重新出现时被调用。并且viewDidAppear 检查了一些变量firstStart,但显然这个firstStart 变量在第一个控制器的初始viewDidAppear 和第一个控制器的第二个viewDidAppear 之间没有正确重置。

您要么需要让第二个控制器以某种方式更新此 firstStart 变量的值(例如通过委托协议),要么在第一个视图重新出现时重新计算 firstStart

【讨论】:

  • 实际上第一个开始是从文件中填充的数组,文件是在第二个 viewcontroller 中创建的(在 JSON 下载和解析之后),所以在第一次调用 secondviewcontroller 之后,文件被创建并且它类似于你所拥有的写了,但无论如何谢谢!
  • 好吧,让我们让事情变得更简单,而不是调用 performSegueWithIdentifier 我从 appdelegate 打开 secondview
  • - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //从文件读取后 if(!self.largeStickerFileNames) { UIStoryboard *storyboard = [UIStoryboard storyboardWithName :@"MainStoryboard" bundle:nil]; JSONDownloadViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"downloadView"]; [(UINavigationController)self.window.rootViewController pushViewController:vc 动画:YES]; } 返回是; }
  • 但仍然无法从第二个视图中解散
  • @DavidGeghamyan 让我们continue this discussion in chat
猜你喜欢
  • 1970-01-01
  • 2021-05-04
  • 2011-09-27
  • 2011-12-26
  • 1970-01-01
  • 2014-10-24
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多