【问题标题】:Cannot change views in splitviewcontroller无法更改拆分视图控制器中的视图
【发布时间】:2011-01-20 18:32:05
【问题描述】:

我试图通过单击模态视图中的按钮来更改拆分视图控制器中的视图(一个人正在选择一个选项)。我正在使用通知来完成此操作:

  1. 当在模态视图中单击按钮时,它会发出通知,然后自行关闭(关闭):

    [[NSNotificationCenter defaultCenter] postNotificationName:@"launchProject" object:nil];

  2. 拆分视图控制器内的 DetailViewController 正在监听此通知,并切换出 SVC 中的视图

-(void)launchProject:(NSNotification *)notification { Project* secondDetail2 = [[Project alloc] initWithNibName:nil bundle:nil]; ProjectRootController* secondRoot2 = [[ProjectRootController alloc] initWithNibName:nil bundle:nil ]; self.splitViewController.viewControllers =[NSArray arrayWithObjects: secondRoot2, secondDetail2, nil]; }

我不明白为什么视图没有切换出来。欢迎对此提出任何建议。

【问题讨论】:

    标签: cocoa-touch uisplitviewcontroller


    【解决方案1】:

    您还没有显示所有代码,所以我猜问题是对通知的工作方式有误解。最初可能会令人困惑,但它非常简单。到目前为止,您已经:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"launchProject" object:nil]

    这很好。

    但你也需要有

    [[NSNotificationCenter defaultCenter]
     addObserver:self 
     selector:@selector(launchProject:) // selector should be your function name, launchProject
     name:@"launchProject" // notification name - must be same as what is given to postNotificatioName.
     object: nil];
    

    某处,例如init 函数中。

    换句话说,postNotificationName:@"launchProject" 不会调用您的函数 launchProject。它将名为“launchProject”的通知放入NSNotificationCenter defaultCenter。如果你不是在寻找那个特定的通知,那么什么都不会发生。

    希望对您有所帮助..

    【讨论】:

    • 很有帮助。感谢您的宝贵时间。
    • @Aaron,您可以通过单击答案左侧的绿色复选标记轮廓来接受答案。请考虑查看此答案和其他问题的答案,如果适用,请单击“接受”复选标记。
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多