【问题标题】:Updating a BOOL value from another class从另一个类更新 BOOL 值
【发布时间】:2013-01-31 23:08:36
【问题描述】:

我已经设置了一个具有以下内容的课程: MESSidePanelViewControllerSubClass 头文件

@property BOOL setLandscapeOK;

Imp 文件

- (NSInteger)supportedInterfaceOrientations {
    // Restriction for the welcome page to only allow potrait orientation

    if (setLandscapeOK == YES) {
        NSLog(@"Reveal-setting all");
        return UIInterfaceOrientationMaskAll;
    }
    NSLog(@"Reveal-setting portrait");
    return UIInterfaceOrientationMaskPortrait;
}

我现在想从另一个文件(视图控制器)更新值。
登录视图控制器 其他视图控制器 imp 文件

- (NSInteger)supportedInterfaceOrientations {
    // Restriction for the welcome page to only allow potrait orientation
    NSLog(@"Login-supportInterfaceOrientations");
    MESSidePanelViewControllerSubClass* setLandscapeOK = YES;
    return UIInterfaceOrientationMaskAll;
}

我收到一个错误:

Implicit conversion of 'BOOL' (aka 'signed char') to 'MESSidePanelViewControllerSubClass *' is disallowed with ARC

我应该如何更新另一个文件中的 BOOL 值?

【问题讨论】:

  • 也许你需要多读一点 what properties and instances are。这真的……很奇怪(将 BOOL 分配给与该类的属性名称相同的类的实例)
  • 而不是if (setLandscapeOK == YES),使用if (self.setLandscapeOK)。您无需将BOOLYES/NO 进行比较。

标签: ios ios5 uiviewcontroller boolean implicit-conversion


【解决方案1】:

MESSidePanelViewControllerSubClass 中的-supportedInterfaceOrientations 不应该总是返回UIInterfaceOrientationMaskPortrait 吗?如果将MESSidePanelViewControllerSubClass 的实例作为子视图控制器推送,那不会强制您的UI 方向为纵向吗?你试过吗?

【讨论】:

    【解决方案2】:

    这一行是错误的:

    MESSidePanelViewControllerSubClass* setLandscapeOK = YES;
    

    它应该是这样的:(mESSidePanelViewControllerSubClass 也可能是一个 iVar,并在您的 supportedInterfaceOrientations 方法之外的某个地方实例化。)

     MESSidePanelViewControllerSubClass *mESSidePanelViewControllerSubClass = [MESSidePanelViewControllerSubClass alloc] init];
    

    然后:

    mESSidePanelViewControllerSubClass.setLandscapeOK = YES;
    

    编辑添加到delegate的链接。

    【讨论】:

    • 这不会创建子类的另一个实例吗?这些是步骤。已创建欢迎视图控制器/已创建导航控制器/已创建子类 Reveal 侧面板,并且已显示导航控制器。欢迎应该只是肖像。登录视图控制器通过按钮加载。登录被推送到导航控制器,该控制器仍保留在显示侧面板中(加载有子类)。登录应该是横向的。我认为上面创建了一个子类的新实例,而不是更新已经创建的实例?
    • 根据您的描述,有几种选择: A. 使用委托模式。 B. 传递 BOOL var 的引用。 C. 使用 NSNotification。但是在我看来,对于这种类型的活动,使用委托是最好的选择(请参阅我编辑的答案,其中包含指向我之前关于此主题的答案之一的链接。)
    • 在 prepareForSegue 的呈现视图控制器上设置委托(如果您使用情节提要,根据我看到的代码,我相当确定您是)并确保您的委托是一个弱属性呈现视图控制器,永远不要保留委托,嗯......除非你有一个非常好的理由......
    猜你喜欢
    • 2013-01-12
    • 2017-10-30
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2023-04-09
    • 2021-03-26
    相关资源
    最近更新 更多