2010-12-09 93 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]; 
} 

如果你的意思是不同的,你可以提供一些代碼,我也許能幫助更多的,但就像我說你原來的問題不是很清楚,我這是不是你的意思;)

相關問題