views:

36

answers:

1

Hi. In short I would like to call a method from a viewSwitchManager class that contains a method which would switch the current view on display to a different view.

-(void)replaceView:(UIView*)oldSubView withView:(UIView*)newSubView;

I am trying to implement the following:

In my project I have 10 views they all have their own .m and .h files. Lets represent each view with number from 1 - 10.

Each view simply contains 9 buttons that would allow the user to click a button and then the app would take the user to the specified view depending on the button clicked.

Example:

So lets say the current view being displayed is: view1. on view1 there are 9 buttons being displayed which each correspond to a different view in the app.

[2] [3] [4] [5] [6] [7] [8] [9] [10]

NOTE: button1 is missing as that responds to view1 which is the current view so button1 is not needed on this view.

Action: The user clicks on button 6. a method would get called from a separate class called viewSwitchManager would replace view1 the current view, with view6 the new view.

the new view which would be view6 would then display again, buttons that correspont to a different view in the app like so:

[1] [2] [3] [4] [5] [7] [8] [9] [10]

NOTE: button6 is missing as that responds to view6 which is the current view so button6 is not needed on this view.

This is what i am trying to achieve. Ive tried different things but i keep getting errors. At one point even a SIGBART error. I've had problems with sending views as parameters, problems with obtaining the current view and replacing it with a new view and so on. I've thought about having a navigation controller but having looked at that looks like a daunting experience and i really dont want to drill down into different layers. Just a simple one in one out view system would be great. lol

Can someone please help me as this has taken up a lot of time! Because this problem took most of my time I've even had to repurchase the license just to buy more time which is quite funny.

Any help will be MUCH appreciated. Thank you in advance.

+1  A: 

It sounds like viewSwitchManager should descend from UIViewController, which I'll call SwitchViewController in my example code below. This has a bland UIView as its view to serve as the backdrop which will always be covered by a button-filled view.

After that, it should be a simple matter of:

@implementation SwitchViewController
-(void) replaceView:(UIView*)oldSubView withView:(UIView*)newSubView {
    [[self view] addSubview:newSubView];
    [oldView removeFromSuperview];
}
@end

I suspect your problems are less involved with managing views as it is with managing memory. If you don't retain it elsewhere, [oldView removeFromSuperview] will release and deallocate the old view.

Two things you can do to make sure your code is more solid. One, build with the analyzer (Cmd-Shift-a) and clean up any issues it finds.

Two, enable Zombies. NSZombieEnabled causes all objects deallocated to be kept in memory, but marked as "deallocated." If a deallocated "zombie" object is ever used, an exception is raised. After that, it's a matter of determining why your objects are not being appropriately retained. Here's how to set NSZombieEnabled:

  • Project -> Edit Active Executable
  • Select the "Arguments" tab
  • Add to "Variables to be set in the environment"
    • Name: NSZombieEnabled
    • Value: YES
John Franklin
thing is... im if i press a button3 in view1, i will be in view1Controller, so i call the replaceView method but how do i call it i have to instantiate a switchViewManager class which seems like im wasting code as i have to instantiate that in every view. Then i cant just add subView just like that as i have to pass in a parameter. Now that parameter could be any of the 9 other views. meaning i also have to instantiate 9 other views in that current view thats being displayed just so that the user can switch between them. It looks like theres far too much code. There must be an easier way
Pavan
i really appreciate your time and effort in posting your answer though if you could help me on what i am trying to achieve i will appreciate it a lot. thanks in advance
Pavan
The SwitchViewController should be external to all the other view controllers, not a part of them. The array of 10 ViewControllers should be owned by the SwitchViewController much like a UITableViewController is owned by a UINavigationController.
John Franklin
ah right i see.... that make so much sense. this way i dont have to keep instantiating the views in the individual views itself. otherwise i would have instantiate it by 10x10 lol.So basically if i am in view1 would i do something likeSwitchViewController *switcher = new blah blah.[switcher replaceViews:self.view view4];see but that doesnt look right.sorry if this may seem obvious its just that ive been pulling my hair with this for a while.
Pavan
You're headed in the right direction.
John Franklin