如何在删除子视图时知道哪个viewController

时间:2010-12-09 05:41:00

标签: iphone objective-c

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

{ if([self quizViewController] != nil) { [self setQuizViewController:nil]; } QuizViewController *quiz = [[QuizViewController alloc] initWithNibName:@"QuizViewController" bundle:nil]; [quiz 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函数中如何检测它是否是一个测验?

3 个答案:

答案 0 :(得分:1)

if([[self view] subviews] objectAtIndex:0] isKindOfClass:CLASS(class)) ...

或isMemberofClass

从内存开始,你必须测试它......

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

someUIView.tag = 99;

然后

if([[self view] subviews] objectAtIndex:0] .tag == 99) ...

干杯

答案 1 :(得分:0)

维护一个UIViewController(例如UIViewController * currentViewController)对象,用于保持当前视图控制器的轨迹。
现在在推送到视图控制器之前,根据我的理解被多个视图控制器推动,你可以使用你正在推动的viewController设置currentViewController。 因此,如果你在QuizViewController中并推送到控制子视图的viewController,你首先设置它的currentController然后推它。
   subViewController.currentController = self;
   [self.navigationController pushViewController:subViewController animated:YES];

答案 2 :(得分: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];
}

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