views:

214

answers:

3

I find myself in need of access to a viewcontroller from its view.

Here is the method

-(void)changePageView:(UIViewController*)newviewcont withtransitiontype:(int)t andtransitionspeed:(int)s
{
    //Remove whatever view is currently loaded at index 0, this index is only to be used by "page" views
    UIView *oldview = [self.view.subviews objectAtIndex:0];

    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:s];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    [newviewcont viewWillAppear:YES];
    //[oldview viewWillDisappear:YES];
    [oldview removeFromSuperview];
    [self.view insertSubview:newviewcont.view atIndex:0];
    //[oldview viewDidDisappear:YES];
    [newviewcont viewDidAppear:YES];

}

Basically, I am trying to write a generic view switch method that is called by the root controller to swap out subviewcontorllers views from the rootcontrollers view.

I pass in a subviewcontroller and am able to remove the current subview. But in order to do proper view switching animation i need access to the current views view controller. Is this the wrong approach and can it be done?

A: 

I believe your approach is wrong. You should look into UINavigationController I believe.

Genericrich
He might not want a Navigation Bar (you can implement view swtiching with out it)
hhafez
You are right. I did have the wrong approach. Essentially I was fighting the MVC paradigm. I am still trying to understand how to apply MVC in some cases, but am at least making progress!
michael
A: 

The changeView() method belongs in the viewcontroller. It would solve you problem of having the view knowing about it's controller (which it shouldn't) and it makes more sense.

Also unless you are doing something fancy in changeView() that can't be done using the methods in a UIViewController object then you should just use it instead, if it is neccesary to implement your own view switching method then you can extend UIViewController instead of implemtning part of the view controlelr in your view.

my 2 cents :)

hhafez
The changepageview method is in the rootview controller. The issue is that I want a handle to the viewcontroller which has a view currently loacted at index 0 of the rootviewcontrollers view. I have access to the view at index 0, but not its view controller.
michael
from OP 'I am trying to write a generic view switch method that is called by the root controller to swap out subviewcontorllers views from the rootcontrollers view.' unless I am misunderstanding your intention behind this function, UIViewcontroller already provides this functionality.
hhafez
+1  A: 

I added a member to the rootcontroller that hold onto the current sub view controller (currentController) and refers to it when a controller swap is done

-(void)changePageView:(UIViewController*)newviewcont withtransitiontype:(int)t andtransitionspeed:(int)s
{


    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:s];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    [newviewcont viewWillAppear:YES];
    [self.currentController viewWillDisappear:YES];
    [self.currentController.view removeFromSuperview];
    [self.view insertSubview:newviewcont.view atIndex:0];
    [self.currentController viewDidDisappear:YES];
    [newviewcont viewDidAppear:YES];
    [UIView commitAnimations];


    self.currentController = newviewcont;

}
michael