引用添加子视图的类

发布于 2024-11-29 19:26:21 字数 313 浏览 0 评论 0原文

我正在向我的主 iPad UIViewController 添加一个子视图,在该子视图中我需要引用所述视图控制器才能使用该控制器播放视频。

任何人都可以帮我解决应该完成的方式,以及可能的代码示例吗?

先感谢您。

此致,

Ed

编辑(更多信息):

此子视图是为 iPhone 设计的 uiviewcontroller 类的视图。这是一个在按下一行时加载视频的表。电影播放器​​在引用的视图控制器中加载视频(这就是为什么我想从子视图中引用 iPad 视图控制器)。该视图基本上以相同的形式在 iPad 应用程序中使用。

I'm adding a subview to my primary iPad UIViewController, and within that subview I need to reference said view controller in order to play a video using that controller.

Can anyone help me out with the way that should be done, and possibly a code example?

Thank you in advance.

Regards,

Ed

EDIT (A LITTLE BIT MORE INFO):

This subview is a view from a uiviewcontroller class that is designed for the iPhone. It's a table that loads a video when a row is pressed. The movieplayer loads the video within the referenced viewcontroller (which is why I want to reference the iPad view controller from within the subview). The view is basically used within an iPad app in it's same form.

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

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

发布评论

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

评论(4

燕归巢 2024-12-06 19:26:21

这听起来像是一个架构问题。告诉某些东西播放声音并不取决于你的观点。这是控制员的工作。由您的视图来告诉“某人”它被触摸、滑动或用户所做的任何事情。然后“某人”(正在观看的人)将对此做出正确的反应。

要做到这一点,你的观点通常应该设定一个目标,并可能采取行动。查看UIControl(例如,UIButton)如何通知其他对象它已被激活。然后观察者(控制器)做出相应的反应。

编辑

视图不应加载视频。视图控制器应该加载视频并将其安装到正确的视图中。视图唯一的工作就是告诉其视图控制器它已被按下。 UITableView 使用 UITableViewDelegate 方法 tableView:didSelectRowAtIndexPath: 自动处理此问题。如果您不使用 UITableView,您仍应遵循此模式。视图接受一个委托并告诉委托(控制器)选择了某些内容。然后控制器用新数据更新视图。

This sounds like an architecture problem. It's not up to your view to tell something to play a sound. That's a controller's job. It's up to your view to tell "someone" that it was touched, or slid, or whatever the user has done. "Someone" (who is watching) will then perform the correct response to that.

To do this, your view should generally take a target, and possibly and action. Look at how UIControl (for example, UIButton) informs other objects that it has been activated. The observer (controller) then reacts accordingly.

EDIT

The view should not load a video. A view controller should load the video and install it into the correct view. The only job the view has is to tell its view controller that it has been pressed. UITableView handles this automatically with the UITableViewDelegate method tableView:didSelectRowAtIndexPath:. If you're not using a UITableView, you should still follow this pattern. The view accepts has a delegate and tells the delegate (controller) that something was selected. Then the controller updates the views with the new data.

小糖芽 2024-12-06 19:26:21

您可能已经拥有位于应用程序委托的属性中的主视图(它通常通过应用程序的 .xib 文件分配,查看应用程序委托的 applicationDidFinishLaunching: 方法,其中它将主视图作为子视图添加到窗口中,诸如此类喜欢:

[window addSubview:primaryController.view];
[window makeKeyAndVisible];

)。

因此,在应用程序中需要访问主视图的任何位置,您都可以执行以下操作:

[(MyAppDelegateType*)([UIApplication sharedApplication].delegate).primaryController somePrimaryControllerMethod];

编辑:虽然这可行,但我同意 Rob Napier 的观点,从架构角度来看,这不是最好的方法。

You probably already have the main view living in a property of your application delegate (it is commonly assigned via the application's .xib file, look in the app delegate's applicationDidFinishLaunching: method, where it adds the main view as a subview to the window, something like:

[window addSubview:primaryController.view];
[window makeKeyAndVisible];

).

So anywhere in your app where you need to access the main view, you can do:

[(MyAppDelegateType*)([UIApplication sharedApplication].delegate).primaryController somePrimaryControllerMethod];

Edit: while this will work, I agree with Rob Napier that this isn't the best way to do it, architecture-wise.

似梦非梦 2024-12-06 19:26:21

您可以使用 [yourSubview superview] 来获取超级视图。

You could just use [yourSubview superview] to get the superview.

夜巴黎 2024-12-06 19:26:21

难道你不能像这样向视图添加一个属性:

@property (nonatomic, assign) UIViewController *parentViewController;

在视图初始化时(从 UIViewController 内),设置属性:

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
customView.parentViewController = self;

从那时起,你应该能够从视图内调用parentViewController。

编辑: Rob Napier 有一个很好的观点,从视图而不是视图控制器中设置目标和选择器可能是更好的主意。这样您应该能够直接将方法与视图连接起来。这些属性看起来像这样:

@property (nonatomic assign) id target;
@property (nonatmic, assign) SEL selector;

也许还可以向您的视图添加一个指定的初始值设定项:

- (id)initWithFrame:(CGRect)aFrame target:(id)aTarget selector:(SEL)aSelector
{
   self = [super initWithFrame:aFrame];
   if (self)
   {
      self.target = aTarget;
      self.selector = aSelector;
   }
   return self;
}

Can't you just add a property to the view like this:

@property (nonatomic, assign) UIViewController *parentViewController;

And on initialization of the view (from within the UIViewController), set the property:

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
customView.parentViewController = self;

And from then on you should be able to call the parentViewController from within the view.

Edit: Rob Napier has a good point, it's probably a better idea to set a target and selector from within the view instead of a view controller. This way you should be able to hook up methods directly with the view. The properties would look like this:

@property (nonatomic assign) id target;
@property (nonatmic, assign) SEL selector;

And perhaps add a designated initializer to your view:

- (id)initWithFrame:(CGRect)aFrame target:(id)aTarget selector:(SEL)aSelector
{
   self = [super initWithFrame:aFrame];
   if (self)
   {
      self.target = aTarget;
      self.selector = aSelector;
   }
   return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文