繁体   English   中英

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

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

我必须调用视图控制器中存在的方法,该方法的引用在视图中可用。 当我尝试像其他方法一样调用该方法时,由于某种原因,iPhone只会忽略该调用。 有人可以解释为什么会发生这种情况,以及如何调用这种方法吗?


在视图中,我有这种方法:

-(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;
    }
}

}

每当触发touches事件时,Hellooooo就会在控制台中打印,但是之前的方法将被忽略

在视图中,我有这种方法:

-(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;
    }
}

}

每当触发touches事件时,Hellooooo就会在控制台中打印,但是之前的方法将被忽略

我为该视图创建了一个视图控制器,并允许视图控制器之间进行通信。 也许这是MVC协议的一部分。

在使用时检查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