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.
主讲人:
- (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;
}
}
}
注
-(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