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

时间:2010-10-13 14:03:25

标签: iphone

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

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

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:1)

HelpViewController中为参数定义一个setter,并将您的代码更改为:

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

答案 2 :(得分:1)

我通常在我的UIView中有一些变量,我从父视图设置。为了传回变量,我使用了函数:

[[[self.navigationController viewControllers] lastObject] setFoo:foo];