iphone / ObjC - Method to remove/add views based on arguments - possible?

193 Views Asked by At

I'm new - i'm sorry - but I'm experimenting with multiview iphone apps, and wondered whether the below idea was a) possible and b) sensible.

I want to create a method that can remove and add views based on some parameters - the outgoing view, the incoming view and the incoming class.

- (void)switchViews:(Class)inView:(Class)outView:(Class)inClass{

inClass *tempView = [[inClass alloc]
                     initWithNibName:@"inView" bundle:nil];


tempView.burgerViewController = self;   

self.inView = tempView;
[tempView release];


[outView.view removeFromSuperview];
[self.view insertSubview:tempView.view atIndex:0];

}

This would be called by:

[burgerViewController switchViews:viewMainMenu:viewOptions:ViewMainMenu];

Any help is much appreciated - I have a lot to learn.

Mike.

2

There are 2 best solutions below

0
On

Your code is wrong, in that (it appears that) you've misunderstood how method names work in Objective-C.

For example, as your method currently stands, it is named:

switchViews:::

That's probably not what you're looking for.

A better name might be:

replaceView:forProperty:withViewOfClass:

Declared, that would look like:

- (void) replaceView:(UIView *)outView forProperty:(NSString *)propertyName withViewOfClass:(Class)inClass;

And you would use it like this:

Class viewOptions = ...;
NSString *viewMainMenu = @"...";
[burgerViewController replaceView:viewMainMenu forProperty:viewMainMenu withViewOfClass:viewOptions];

For more on Objective-C method names and interleaved arguments, check out the Objective-C Programming Language Reference.

0
On

Well, your first problem is that you release tempView and then attempt to insert it into the view. Don't release tempView at all, just keep it as-is for insertion into the main view.