views:

71

answers:

3

hi, i have a uiviewcontroller named MainViewController, i add a subview in it (name:DetailView) in viewdidload event. when i press the a button in mainviewController i want to call a method in subview, writeSomething. //DetailView is a subclass of a UIView.

in Mainviewcontroller:

    - (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"DetailView" owner:self options:nil];
    UIView *aView= [nibViews objectAtIndex:0];
    aView.tag=100;
    self.detailView=aView;
    [detailContainer addSubview:self.detailView];


    }

-(void) writeAnythingInSubViewsLabel{
//i tried to reach detail UIView
 NSArray* subviews = [NSArray arrayWithArray: detailContainer.subviews];
 for (UIView *xView in subviews) {
  if ([xView isKindOfClass:[UIView class]]  && xView.tag==100) {
   NSLog(@"%@", xView);
   [xView writeSomething:@"hello"];
   break;
  }
 }
}

in DetailView:

-(void) writeSomething:(NSString *)anyText{
NSLog(@"%@", anyText);
}

but it gives error; [UIView writeSomething]: unrecognized selector sent to instance.... it also gives a warning before build, UIView may not respond writeSomething... i kknow UIView and DetailView doesnt match. thats why it gives warning. i tried also for (DetailView *xView in subviews) { but it never finds the tag in the loop.

i hope anyone help me... EDIT: DetailView.m

#import "DetailView.h"


@implementation DetailView
@synthesize image, button, label;
@synthesize delegate;

- (void)dealloc {
    [image release];
    [button release];
    [label release];
    [super dealloc];
}


- (id) initWithCoder: (NSCoder *) decoder {
    return self;
}


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(IBAction) backButtonPressed:(id) sender{
    [delegate hideDetailView];

}

-(void) writeSomething:(NSString *)anyText{
    NSLog(@"%@", anyText);
}

@end
A: 

Try [detailContainer viewWithTag:100]

Nir Levy
i tried viewWithTag method, it gives same error(unrecognized selector). i already saved it in viewdidload method. when i use [self.detailView writeSomething:@"aaa"];, same error. i think DetailView.h class doesnt connect to xib file. because when i connect buttons in IB, then compile it says"this class is not key value coding-compliant for the key button.." i think i miss something while loading uiview with [NSBundle mainBundle] loadNibNamed method.
tester
A: 

Not sure what the problem is, but why not save the created view in a member? This way it will be accessible to you without having to re-search for it.

Danra
He actually does that already in viewDidLoad -- somehow he forgot about that and decided to locate the view by tag -- mixture of leftovers from desperate attempts, I guess
Till
yes you are right. i forgot it. but i stil suspect loadNibNamed.there are just default initWithFrame method, @synthesize and dealloc method in DetailView.m file. i design the view in IB. I set Files owner to DetailView class. but i dont know why it gives error.
tester
A: 

It seems that you are creating a UIView rather than a DetailView.

As a result, it is not finding the method you created in DetailView, since it thinks that xView is actually a UIView - which doesn't have the writeSomething method.

You should go back to your header, import the DetailView.h file, and make sure that detailView is actually an object of type DetailView, rather than UIView.

In viewDidLoad, instead of

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"DetailView" owner:self options:nil];
UIView *aView= [nibViews objectAtIndex:0];
aView.tag=100;
self.detailView=aView;

try using:

self.detailView = [[DetailView alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];

Then, instead of the loop and such in the .m file, you can simply use:

[self.detailView writeSomething:@"words"];

Good luck!

Andrew Natoli