views:

41

answers:

1

Hi all,

I have created three UIImageViews. I then added a UITextField to each image as a subview.

When the user clicks on the text field I want to capture which UIImageView ths text field belongs to. I am trying the following code :

-(void)textFieldDidBeginEditing : (UITextField *)textField {


textField = retainedObject;

MyPicture *capture = nil;

for (UIImageView *oneView in retainedObject.superview) {

    if ([oneView isMemberOfClass:[myPicture class]])
    capture = (UIImageView *)oneView;

}

However I get an error that UIView may not respond to count by enumeration. It looks like the superview of the textfield is UIView rather than UIImageView perhaps ? Can anybody help solve this ?

Thanks,

Martin

+1  A: 

A UIView's superView is assumed to be a UIView. However, you can still cast it as a UIImageView, and call UIImageView methods on it, but just make sure it will always be a UIImageView without exception, as otherwise it will crash.

Source: My own applications ;)

jrtc27
Thanks - but I'm confused by what you mean by cast to ImageView. The superview of the UITextField is already a UIImageView ?
Ohnomycoco
Just add `UIImageView *superImageView = (UIImageView *)[retainedObject superView];`, and then you can easily call all `UIImageView` functions on the object.
jrtc27
Thanks again - appreciate your help. I am now getting an error that UITextField (here retainedObject) may not respond to superView ?
Ohnomycoco
Ah - superView should be superview - lets try again.
Ohnomycoco