6
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?

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
Jules
  • 7,568
  • 14
  • 102
  • 186

3 Answers3

13

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
  • 18,461
  • 9
  • 58
  • 95
  • Ok, what about the enum, I've not created one yet. – Jules Oct 13 '10 at 14:15
  • typedef enum { VAL1, VAL2, VAL3 } MyType; – jhabbott Oct 13 '10 at 14:18
  • I was hoping it would give me an error in xcode if I changed initWithNibName – Jules Oct 13 '10 at 14:39
  • 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 Oct 13 '10 at 15:59
1

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];
tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
1

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
  • 14,517
  • 25
  • 92
  • 153
  • Can you give me a SetFoo function which uses an enum ? http://img.skitch.com/20101013-cd4ty65e3ns4d45hqjc71k1sn.jpg Also what lastObject – Jules Oct 13 '10 at 14:43
  • 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 Oct 13 '10 at 14:58
  • Erm, confused, so how do I setup setFoo ? I thought I was making some progress, now I have no clue. – Jules Oct 13 '10 at 15:17
  • You didn't say what lastobject was either :( – Jules Oct 13 '10 at 16:00