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

这是显示我的观点的代码。我知道我可以使用应用程序委托(delegate)变量,但是如果我可以以某种方式传递参数,最好是使用枚举,那会更整洁。这可能吗?

最佳答案

只需为您的HelpViewController创建一个新的init方法,然后从那里调用其 super 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

在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;
}

并称之为:
UIViewController *theController = [[HelpViewController alloc] initWithNibName:@"HelpView" bundle:nil onPage:PAGE1];
[self.navigationController presentModalViewController:theController animated:YES];
[theController release];

关于iphone - 如何在iOS的 View 中传递参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3924522/

10-12 14:44