简体   繁体   English

从视图中调用视图控制器中的方法

[英]Calling a method in a view controller from a view

I have to invoke a method present in a view controller who's reference is available in the view. 我必须调用视图控制器中存在的方法,该方法的引用在视图中可用。 When I try to call the method like any other method, for some reason, iPhone just ignores the call. 当我尝试像其他方法一样调用该方法时,由于某种原因,iPhone只会忽略该调用。 Can somebody explain as to why this happens and also how can I go about invoking this method? 有人可以解释为什么会发生这种情况,以及如何调用这种方法吗?


In the view I have this method: 在视图中,我有这种方法:

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event{
NSArray* mySubViews = [self subviews];
for (UITouch *touch in touches) {
    int i = 0;
    for(; i<[mySubViews count]; i++){
        if(CGRectContainsPoint([[mySubViews objectAtIndex:i] frame], [touch locationInView:self])){
            break;
        }
    }
    if(i<[mySubViews count]){
                    // viewController is the reference to the View Controller. 
        [viewController pointToSummary:[touch locationInView:self].y];
        NSLog(@"Helloooooo");
        break;
    }
}

} }

Whenever the touches event is triggered, Hellooooo gets printed in the console but the method before that is simply ignored 每当触发touches事件时,Hellooooo就会在控制台中打印,但是之前的方法将被忽略

In the view I have this method: 在视图中,我有这种方法:

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event{
NSArray* mySubViews = [self subviews];
for (UITouch *touch in touches) {
    int i = 0;
    for(; i<[mySubViews count]; i++){
        if(CGRectContainsPoint([[mySubViews objectAtIndex:i] frame], [touch locationInView:self])){
            break;
        }
    }
    if(i<[mySubViews count]){
                    // viewController is the reference to the View Controller. 
        [viewController pointToSummary:[touch locationInView:self].y];
        NSLog(@"Helloooooo");
        break;
    }
}

} }

Whenever the touches event is triggered, Hellooooo gets printed in the console but the method before that is simply ignored 每当触发touches事件时,Hellooooo就会在控制台中打印,但是之前的方法将被忽略

I created a view controller for the view and allowed communication between the view controllers. 我为该视图创建了一个视图控制器,并允许视图控制器之间进行通信。 Perhaps this is part of the MVC protocol. 也许这是MVC协议的一部分。

Check the value of viewController at the point where it is used to make sure its not nil, use the debugger or add NSLog: 在使用时检查viewController的值以确保其不为nil,使用调试器或添加NSLog:

[viewController pointToSummary:[touch locationInView:self].y];
NSLog(@"viewController=%@", viewController);
NSLog(@"Helloooooo"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM