Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a parameter into a view in iOS?

Tags:

iphone

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?

like image 684
Jules Avatar asked Oct 13 '10 14:10

Jules


People also ask

How do you pass a parameter in Swift?

To pass a function as a parameter into another function in Swift, the accepted parameter type needs to describe a function. Now you can call this function by passing it an argument that is any function that takes two doubles and returns a double. This is the quick answer.

How do Views work in SwiftUI?

While UIKit uses an event-driven framework, SwiftUI is based on a data-driven framework. In SwiftUI, views are a function of state, not a sequence of events (WWDC 2019). A view is bound to some data (or state) as a source of truth, and automatically updates whenever the state changes.

What is a parameter in Swift?

Parameters can provide default values to simplify function calls and can be passed as in-out parameters, which modify a passed variable once the function has completed its execution. Every function in Swift has a type, consisting of the function's parameter types and return type.


1 Answers

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];
like image 119
jhabbott Avatar answered Sep 20 '22 10:09

jhabbott