tags:

views:

71

answers:

3
UIViewController *theController = [[HelpViewController alloc] initWithNibName:@"HelpView" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];

Here's my code for showing my view. I know I can use app delegate variables, but it would be neater is I could pass a parameter in somehow, ideally using an enum. Is this possible?

A: 

Define a setter for the parameter in HelpViewController and change your code to:

HelpViewController *theController = [[HelpViewController alloc] initWithNibName:@"HelpView" bundle:nil];
[theController setSomeValue:@"fooBar"];
[self.navigationController presentModalViewController:theController animated:YES];
tob
Ok, what about the enum, I've not created one yet.
Jules
Shouldn't it be 'YES', instead of 'TRUE'?
mshsayem
Hmmm typedef enum { VAL1, VAL2, VAL3 } HelpPages; -(void) setHelpPage:(HelpPages)value {..... I have something wrong here
Jules
http://img.skitch.com/20101013-cd4ty65e3ns4d45hqjc71k1sn.jpg
Jules
+3  A: 

Just create a new init method for your HelpViewController and then call its super init method from there...

In HelpViewController.h

typedef enum
{
    PAGE1,
    PAGE2,
    PAGE3
} HelpPage;

@interface HelpViewController
{
    HelpPage helpPage;
    // ... other ivars
}

// ... other functions and properties

- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle onPage:(HelpPage)page;

@end

In HelpViewController.m

- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle onPage:(HelpPage)page
{
    self = [super initWithNibName:nibName bundle:nibBundle];
    if(self == nil)
    {
        return nil;
    }

    // Initialise help page
    helpPage = page;
    // ... and/or do other things that depend on the value of page

    return self;
}

And to call it:

UIViewController *theController = [[HelpViewController alloc] initWithNibName:@"HelpView" bundle:nil onPage:PAGE1];
[self.navigationController presentModalViewController:theController animated:YES];
[theController release];
jhabbott
Ok, what about the enum, I've not created one yet.
Jules
typedef enum { VAL1, VAL2, VAL3 } MyType;
jhabbott
I was hoping it would give me an error in xcode if I changed initWithNibName
Jules
You're not changing initWithNibName:bundle: - you're creating a new init method called initWithNibName:bundle:extraParam: from inside that method you can do whatever you like with the extra parameter. The code in my answer is the code you put in HelpViewController.m and you also need to declare the method in HelpViewController.h - of course you can change the declaration to: - (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle onPage:(HelpPages)page; if it helps make more sense to you.
jhabbott
A: 

I generally just have certain variables in my UIView, which I set from the parent view. To pass variables back, I make use of the function:

[[[self.navigationController viewControllers] lastObject] setFoo:foo];
Jason
Can you give me a SetFoo function which uses an enum ? http://img.skitch.com/20101013-cd4ty65e3ns4d45hqjc71k1sn.jpgAlso what lastObject
Jules
As you can see, you need to declare your function differently, as `-(void)setHelpPage:(HelpPages*)value;`. However, I wouldn't suggest overriding the setter function unless you really have to. You can much more easily use `@synthesize` to automatically set up this ability to set parameters.
Jason
Erm, confused, so how do I setup setFoo ? I thought I was making some progress, now I have no clue.
Jules
You didn't say what lastobject was either :(
Jules