ios - 展开 segue/performSegueWithIdentifier : problems

标签 ios objective-c xcode

在我的应用程序中,有一个聊天服务。当用户想给新 friend 发消息时,他按下“新聊天”,然后会显示一个 TableView Controller 以从 friend 列表中进行选择。我正在使用 unwind segues 以返回到以前的 View Controller 并在两个 View Controller 之间共享数据(主要是数据将成为聊天的 friend )。 unwind segue 完美运行,但是在我的应用程序中,当用户返回主 VC 时,我触发一个新的 segue 以便直接转到用户聊天的另一个 VC;这是行不通的。所有 segues 都连接良好,我在每个角落都尝试了 NsLog,它正在进入那里,甚至 prepareForSegue: 正在被访问。我尝试放置一个 NsTimer,认为可能存在一些技术冲突,但没有成功。我将聊天 VC 的 segue 更改为模式,现在它给了我这个错误:

Warning: Attempt to present on whose view is not in the window hierarchy!

我可以通过其他方式访问这个 VC,它在层次结构中。我的问题是:可能出什么问题了? unwind segues 会改变 Windows 层次结构吗?

图片: enter image description here

为了更直观地呈现问题,我正在谈论的主要 VC 在左下角连接到导航 View Controller 。当用户按下新聊天时,会显示右上角的两个 VC(一个用于在 friend 或群组之间进行选择,另一个用于显示 friend /群组)。因此,当从右上角的 VC 选择一个 friend 说时,我应该放松到主 VC。正如您可以从主 VC 中看到的那样,还有其他 segues。如果我放松 segue,它们都不能工作,如果我正常运行,它们就可以工作。

最佳答案

它不工作并给你错误的原因是因为事情没有按照你认为的顺序发生。

当展开发生时,可见的 View 还没有被关闭,因此您正在尝试对实际上不在该层次结构中的 View 执行 segue,就像错误所说的那样,例如,将 NSLog 语句放在最终 View ,然后在主视图 Controller 的 unwind 方法中,您可以看到以下内容:

2013-11-27 14:51:10.848 testUnwindProj[2216:70b] Unwind
2013-11-27 14:51:10.849 testUnwindProj[2216:70b] Will Appear
2013-11-27 14:51:11.361 testUnwindProj[2216:70b] View Did Disappear

因此主视图中的展开被调用, View 将出现(您的主视图 Controller ),然后您的可见 View 被关闭。这可能是一个简单的修复:

@interface ViewController () {
    BOOL _unwindExecuted;
}

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"Will Appear");
    if (_unwindExecuted) {
        _unwindExecuted = NO;
        [self performSegueWithIdentifier:@"afterunwind" sender:self];
    }
}

- (IBAction)unwind:(UIStoryboardSegue *)segue
{
    NSLog(@"Unwind");
    _unwindExecuted = YES;
}

关于ios - 展开 segue/performSegueWithIdentifier : problems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20250481/

相关文章:

ios - XCode 6.3 Init() 不再编译

ios - 如何刷新collectionView

ios - 在 Objective C/Swift 中,抛出错误时在方法中返回什么?

ios - node-apn 库中出现奇怪的语法错误

iOS/objective-C : Correct Way of Obtaining Meta Class Object

ios - FMDB 无法识别新数据库

ios - 读取 Firebase Auth 抛出错误(Firebase 3.x 和 Swift)

javascript - 将 Javascript 变量传递给 Objective-C

objective-c - 使用UIImagePickerController获取图片——如何判断保存的是PNG还是JPEG?

iphone - UI标签分割