开发者

Reference the class that a subview was added from

开发者 https://www.devze.com 2023-03-28 18:25 出处:网络
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.

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 开发者_StackOverflow社区(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.


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.


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.


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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消