2010-12-09 92 views
0

如何知道哪个的viewController当子视图除去

 
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{ 
    [[self model] setTransitioning:NO]; 
    [[[[self view]subviews] objectAtIndex:0] removeFromSuperview]; 
} 

How can I tell what kind of viewController controls the subview at index 0? If it's a QuizViewController I need to call a function on it.

Thanks in advance.

Good answers. I don't believe I explained enough. This is a all in a view controller that is a view stack. The view controller adds and deletes views manually with an animated transition. I am not using a navigationController and cannot in this particular instance for other reasons.
Sometimes the views I add are simple UIImageViews. Sometimes they are QuizViews. The QuizViews have a QuizViewController because they need internal functionality. Here are the two functions I use to add the views.

- (void)loadQuiz:(NSInteger)quizNum 

{ 如果([自quizViewController] =零!) { [自setQuizViewController:无]; } QuizViewController * quiz = [[QuizViewController alloc] initWithNibName:@“QuizViewController”bundle:nil]; [测验setUp:quizNum]; [self setQuizViewController:quiz]; [quiz release];

[[self view] addSubview:[[self quizViewController]view]]; 
[self setSlide1:[[[self view] subviews] objectAtIndex:0]]; 
[self setSlide2:[[[self view] subviews] objectAtIndex:1]]; 
[[self slide1] setHidden:NO]; 
[[self slide2] setHidden:YES]; 

[self performTransition]; 

}

- (void)loadImage:(NSString *)slideImage 

{

UIImage *tempImg = [UIImage imageWithContentsOfFile:[Utilities localPathForFileName:slideImage]]; UIImageView *temp = [[UIImageView alloc] initWithImage:tempImg]; [[self view] addSubview:temp]; [temp release]; //[topView release]; if ([[[self view]subviews] count] > 2) { //add the 2nd subview //[[[[self view]subviews] objectAtIndex:0] removeFromSuperview]; } [self setSlide1:[[[self view] subviews] objectAtIndex:0]]; [self setSlide2:[[[self view] subviews] objectAtIndex:1]]; [[self slide1] setHidden:NO]; [[self slide2] setHidden:YES]; NSLog(@"%s %d",__FUNCTION__,[[[self view]subviews] count]); [self performTransition];

}

所以我的问题仍然是,在animationDidStop功能,我怎样检测,如果它是一个测试吗?

回答

1

如果([自我视图]子视图] objectAtIndex:0] isKindOfClass:CLASS(类)) ...

或isMemberofClass

从内存中,因此你必须对它进行测试。 ..

您也可以在创建视图时在视图上设置标记,然后在检索视图时查找视图。

someUIView.tag = 99;

然后

如果([自我视图]子视图] objectAtIndex:0] == .TAG 99)

...

干杯

+0

很好的答案。我不相信我足够的解释。 – intomo 2010-12-11 06:58:57

+0

谢谢。这工作。 – intomo 2010-12-11 21:28:11

0

你保持一个UIViewController中(例如对于*的UIViewController currentViewController)对象,用于保持当前视图控制器的轨迹。
现在,在按照我的理解被多个视图控制器推入的视图控制器之前,您需要使用您正在推送的viewController来设置currentViewController。
所以,如果你在QuizViewController中,并推动viewController哪些控制子视图,你首先设置它的currentController,然后推它。
subViewController.currentController = self;
[self.navigationController pushViewController:subViewController animated:YES];

0

视图本身并没有视图控制器附加到它们,所以你的问题措辞的方式不太合理。如果你正在做的是将一个UIView子类(比如说QuizView)作为一个子视图,并且需要知道何时该子类被删除并对其执行操作,那么代码将如下所示;

-(void) animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag 
{ 
    [[self model] setTransitioning:NO]; 
    UIView *subview = [self.view.subviews objectAtIndex:0]; 
    if([subview isKindOfClass:[QuizView class]]) 
    { 
     [(QuizView*)subview yourFunction]; 
    }  

    [subview removeFromSuperview]; 
} 

如果你的意思是不同的,你可以提供一些代码,我也许能帮助更多的,但就像我说你原来的问题不是很清楚,我这是不是你的意思;)

相关问题