How to call a method in a sub UIView?
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;
}开发者_JS百科
}
}
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
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!
Try [detailContainer viewWithTag:100]
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.
精彩评论