0

晚上好,问题来了,[self.view addSubview:pauseView]; 在将 pauseView 加载到当前视图之前,将 BOOL isPaused 设置为 false,然后出现子视图。我正在尝试将变量的值从使用 pauseview 更改为 false,但由于它不在当前类中,因此我无法执行此操作。我知道这个主题已经包含在 stackoverflow 中,但我仍然无法解决我的问题。如果我能够解决这个问题,它将在我的其他 3 个应用程序中解决同样的问题。

此致 Sonic555gr

4

2 回答 2

0

将 isPaused 定义为定义 isPaused 的类中的一个属性(我们称之为 MasterView):


// inside MasterView.h
@property (nonatomic,assign) BOOL isPaused;

然后让您的子视图 pauseView 成为自定义 UIView 子类(我们称之为 PauseView),并在此子类中定义一个名为 MasterView 类型的主属性:


// inside PauseView.h
@property (nonatomic,assign) MasterView *master

然后,当您分配/初始化您的 pauseView 时,只需设置此属性:


// somewhere inside MasterView.m
PauseView *pauseView = [[PauseView alloc] initWithFrame:frame];
pauseView.master=self;

最后,在您的 PauseView 类中,在您要更改 isPaused 属性的代码点中,执行以下操作:


// somewhere in PauseView.m
master.isPaused=YES
于 2011-02-10T20:32:55.150 回答
0

您确实应该考虑一下您的架构并尝试将应用程序逻辑从 UIViews 移回控制器(即委托可能是一个不错的选择,但如果不查看更多代码以及您要实现的目标就不可能知道)。

如果您坚持从 UIView 操作变量,则需要在初始化时将 viewController 的引用传递给 pauseView。

因此,在您的 PauseView 类中,您将创建一个自定义初始化程序:

-(id)initWithFrame:(CGRect)frame andViewController:(id)vc {
   self = [super initWithFrame:frame];
   if (self) {
       // Any other custom initialisation here
   }
}
于 2011-02-10T20:34:24.790 回答