从子视图更新主视图上的 IBOutlet

发布于 2025-01-01 04:32:30 字数 499 浏览 1 评论 0原文

我有一个主视图,上面有一个 UISlider。

从主视图中,我使用以下方法添加子视图:

gameView *myViewController = [[gameView alloc] initWithNibName:@"gameView" bundle:nil];
[self.view addSubview:myViewController.view];

子视图创建在主视图之上。

当我使用以下方法删除子视图时:

[self.view removeFromSuperview];

下面的主视图变得可见。

我希望能够在调用 [self.view removeFromSuperview] 之前从子视图更新主视图上 UISlider 的值,

这可能吗?

基本上,问题可以概括为如何从子视图更新主视图上的 IBOutlet。

非常感谢您的帮助。 非常感谢!

I have a main view which has a UISlider on it.

From the main view I add a subview using:

gameView *myViewController = [[gameView alloc] initWithNibName:@"gameView" bundle:nil];
[self.view addSubview:myViewController.view];

The subview is created on top of the main view.

When I remove the sub view using:

[self.view removeFromSuperview];

the main view underneath becomes visible.

I want to be able to update the value of the UISlider on the main view, from the sub view, before I call [self.view removeFromSuperview]

Is it possible?

Basically the question can be generalized to how to update an IBOutlet on the main view from the sub view.

Help is greatly appreciated.
Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2025-01-08 04:32:30

是的,这是可能的。

有几种方法可以做到这一点。我的做法如下:

首先,将父视图控制器的 UISlider 设为可由其他对象访问的属性。

其次,为您的 gameView 对象提供一个实例变量,您将链接到父视图(让我们称之为 id savingParent;

然后,在执行 removeFromSuperview< 之前/code>,您可以简单地执行以下操作:

ParentViewController * parentVC = (ParentViewController *) savedParent;
if(parentVC)
{
    // some float value of whatever you want to set the slider value to
    parentVC.slider.value = 0.5f; 
}

另外,如果您只想添加子视图,为什么要实例化整个视图控制器对象(gameView)?当您执行 removeFromSubview 调用时,视图会被删除,但您的 gameView 视图控制器不会被释放(甚至可能会丢失并泄漏在内存中,从而导致碰撞)。如果你想做一个子视图,子类UIView。如果要推送新的视图控制器,请推送整个控制器(而不仅仅是它包含的视图)。

Yes, it's possible.

And there's a few ways to do this. Here's how I would do it:

First, make your parent view controller's UISlider a property that can be accessed by other objects.

Secondly, give your gameView object an instance variable that you'll link to the parent view (let's call it id savedParent;)

Then, before you do removeFromSuperview, you can simply do something like:

ParentViewController * parentVC = (ParentViewController *) savedParent;
if(parentVC)
{
    // some float value of whatever you want to set the slider value to
    parentVC.slider.value = 0.5f; 
}

Also, why are you instantiating a whole View Controller object (gameView) if you simply want to add a subview? When you do your removeFromSubview call, the view gets removed but your gameView view controller isn't released (and might even be getting lost & leaked in memory, leading to a crash). If you want to do a subview, subclass UIView. If you want to push a new view controller, push the whole controller (and not just the view it contains).

小忆控 2025-01-08 04:32:30

这是另一种方法:

我不确定滑块代表什么,但您需要创建一个代表这个的对象,

@interface MyGameThing : NSObject
@property (assign) CGFloat myValue;
@end 

@implementation MyGameThing {
   CGFloat *_value;
}
@synthesize myValue = _myValue;
@end

然后需要将该对象传递给两个视图控制器(或使其成为单例)。

然后,在 ParentViewController 上的 viewWillAppear 中,只需将滑块设置为新值即可。

丹尼尔.

(ps不要只是将视图控制器视图添加到超级视图,使用presentModalViewController/dismissModalViewController或导航控制器)。

Here is another way:

I'm not sure what the slider is representing, but you need to create an object that represents this

@interface MyGameThing : NSObject
@property (assign) CGFloat myValue;
@end 

@implementation MyGameThing {
   CGFloat *_value;
}
@synthesize myValue = _myValue;
@end

You then need to pass that object to both of your view controllers (or make it a singleton).

Then, on ParentViewController, in the viewWillAppear, just set the slider to the new value.

Daniel.

(p.s. don't just add view controllers views to the superview, use presentModalViewController / dismissModalViewController or a navigation controller).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文