如何将参数传递到iOS中的视图?

Jul*_*les 6 iphone

UIViewController *theController = [[HelpViewController alloc] initWithNibName:@"HelpView" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];
Run Code Online (Sandbox Code Playgroud)

这是我显示我的观点的代码.我知道我可以使用app委托变量,但它更简洁,我可以以某种方式传递参数,理想情况下使用枚举.这可能吗?

jha*_*ott 13

只需为HelpViewController创建一个新的init方法,然后从那里调用它的超级init方法......

在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
Run Code Online (Sandbox Code Playgroud)

在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;
}
Run Code Online (Sandbox Code Playgroud)

并称之为:

UIViewController *theController = [[HelpViewController alloc] initWithNibName:@"HelpView" bundle:nil onPage:PAGE1];
[self.navigationController presentModalViewController:theController animated:YES];
[theController release];
Run Code Online (Sandbox Code Playgroud)