English 中文(简体)
• 如何在“情报调查”的分点上使用某种方法?
原标题: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.

主讲人:

    - (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
最佳回答

看来,你正在形成《意见书》,而不是《详细意见》。

因此,它没有找到你在《详细意见》中采用的方法,因为它认为xView实际上是一种“统一意见”——没有书写方法。

你应回头,进口DetailView.h文档,并确保详细意见实际上属于“详细意见”的一类,而不是“国际调查”。

In viewDidLoad,

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

审判:

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

然后,你可以简单地使用:

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

亲爱!

问题回答

Try [detailContainer view WithTag: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.





相关问题
Python: Binding method

In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance. import types class Base: def payload(self, *args)...

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Behaviour of final static method

I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance....

Setting the Page-Title in .Net using C# from a Class

Question, How do i set the title of a page from a class. Is it even possible? I can and have set the page title from a page itself and a usercontrol. Can I, How Do I do this via a class using C# ....

how to drag in the animation in iphone application?

Iphone application is using the static co-ordinates to reach at some points in the application,which is based on the button event. But what i want is when i drag the item than it should reach at some ...

热门标签