简体   繁体   English

从子视图Objective-c检索值

[英]Retrieving values from subviews Objective-c

I have created a subclass of a UIView which is a rotating wheel. 我创建了一个UIView的子类,它是一个旋转轮。 This wheel is part of another UIView which has an image in the top right corner. 这个轮子是另一个UIView的一部分,它在右上角有一个图像。 When the wheel is turned, in the touchMoved function the right most item is returned (its number). 转动滚轮时,在touchMoved功能中返回最右侧的项目(其编号)。 I need to then change the parent's image automatically when the subview is turned (when the number is returned) 我需要在转动子视图时自动更改父图像(返回数字时)

Assuming I have this number in the subview, how may I return this to the parent, or change the image (in the parent UIView) from the subview? 假设我在子视图中有这个数字,我怎么能将它返回到父视图,或者从子视图中更改图像(在父UIView中)?

Thanks 谢谢

First, create a protocol which defines the method(s) you want to call in your superview: 首先,创建一个协议,该协议定义要在超级视图中调用的方法:

@protocol wheelViewDelegate {
   -(void)doSomething;
}

Your superview needs to implement this protocol: 您的superview需要实现此协议:

@interface superView:UIView<wheelViewDelegate> {
...
}
...
@end

Also, you obviously need to implement the doSomething method. 另外,您显然需要实现doSomething方法。

The UIView, which contains the wheel needs to be a subclass of UIView and hold the delegate, like this: 包含轮子的UIView需要是UIView的子类并保存委托,如下所示:

@interface WheelView : UIView {
id<wheelViewDelegate> delegate;
...
}
@propery (nonatomic, assign) id<wheelViewDelegate> delegate;
...
@end

Don't forget to @synthesize id; 不要忘记@synthesize id; in your implementation. 在你的实施中。

Now, you can call the doSomething in your superview from the subview: 现在,您可以从子视图中调用doSomething视图中的doSomething

[self.delegate doSomething];

EDIT: 编辑:

Okay, I thought this was obvious, but, of course, you need to set the delegate like this: 好吧,我认为这很明显,但是,当然,您需要像这样设置委托:

//In your superView:
WheelView* wv = [WheelView initSomeHow];
wv.delegate = self;

The delegate method mentioned by Phlibbo is one way to do this. Phlibbo提到的委托方法是执行此操作的一种方法。 Since your custom view subclass is more like a control with a value, I would subclass UIControl instead of UIView and use the UIControlEventValueChanged event. 由于您的自定义视图子类更像是带有值的控件,因此我将UIControl而不是UIView并使用UIControlEventValueChanged事件。

In your custom subclass notify the value has changed (maybe in your touchedEnded:withEvent: ): 在您的自定义子类中,通知值已更改(可能在您的touchedEnded:withEvent:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

Now observe value changes in your controller, and let the controller update the image in the other view. 现在观察控制器中的值变化,让控制器在另一个视图中更新图像。

- (void) viewDidLoad {
    [wheelControl addTarget:self action:@selector(wheelValueChanged:) forControlEvents:UIControlEventValueChanged];
}

- (void) wheelValueChanged:(id)sender {
    // update your image based on the wheel value of sender (wheelControl in this case).
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM