如何在打开时将数据从父视图传递给子视图?

发布于 2024-09-13 19:30:58 字数 150 浏览 2 评论 0原文

我想在呈现 modalView 时将数据(字符串数组)从父视图加载到子视图中的一组 UITextField 中。

我知道如何从孩子传递到父母,而且我确信从另一个方向传递会更容易,但我不知道如何做。

更新:更新被删除,因为我发现了问题(模态视图的双重释放)

I want to load data (an array of strings) from the parent view into a set of UITextFields in the child view upon presenting the modalView.

I know how to pass from child to parent, and I'm sure it's even easier to go the other way, but I don't know how.

UPDATE: Update removed because I found the problem (double releasing of modal view)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦断已成空 2024-09-20 19:30:58

重写子视图控制器的 init 方法。

- (id) initWithStrings:(NSArray *)string {
    if (self = [super init]) {
        // Do stuff....
    }
    return self;
}

然后在父级中:

MyChildViewController *vc = [[[MyChildViewController alloc] initWithStrings: strings] autorelease];

Override the init method for the child view controller.

- (id) initWithStrings:(NSArray *)string {
    if (self = [super init]) {
        // Do stuff....
    }
    return self;
}

Then in the parent:

MyChildViewController *vc = [[[MyChildViewController alloc] initWithStrings: strings] autorelease];
放肆 2024-09-20 19:30:58

有两种方法可以做到这一点:

1.按照 Matt 的建议重写 init 方法

2.在子类中创建字段并将这些值传递到文本字段。

@interface ChildViewController : UIViewController{
    NSArray *strings;
    UITextfield *textField1;
    UITextfield *textField2;
}
...

- (void)viewDidLoad {
    [super viewDidLoad];
    textField1.text = [strings objectAtIndex:0];
    textField2.text = [strings objectAtIndex:1];
}

然后在父类中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChildViewController *childController = [[ChildViewController alloc] init];
    childController.strings = your_array_of_strings;
    [self.navigationController pushViewController:childController animated:YES];
    [childController release];

}

Two ways you could do it:

1.Override the init method as Matt suggests

2.Create fields in your child class and pass those values to your text field.

@interface ChildViewController : UIViewController{
    NSArray *strings;
    UITextfield *textField1;
    UITextfield *textField2;
}
...

- (void)viewDidLoad {
    [super viewDidLoad];
    textField1.text = [strings objectAtIndex:0];
    textField2.text = [strings objectAtIndex:1];
}

Then in the parent class:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChildViewController *childController = [[ChildViewController alloc] init];
    childController.strings = your_array_of_strings;
    [self.navigationController pushViewController:childController animated:YES];
    [childController release];

}
苦妄 2024-09-20 19:30:58
- (id)initWithDataObject:(YourDataObjectClass *)dataObject {
    if (self = [super init]) {
        self.dataObject = dataObject;
        // now you can do stuff like: self.myString = self.dataObject.someString;
        // you could do stuff like that here or if it is related to view-stuff in viewDidLoad
    }
    return self;
}
- (id)initWithDataObject:(YourDataObjectClass *)dataObject {
    if (self = [super init]) {
        self.dataObject = dataObject;
        // now you can do stuff like: self.myString = self.dataObject.someString;
        // you could do stuff like that here or if it is related to view-stuff in viewDidLoad
    }
    return self;
}
避讳 2024-09-20 19:30:58

如果您想变得非常奇特,您可以为您的子视图创建一个委托。

@protocol MyChildViewDelegate
- (NSArray*)getStringsForMyChildView:(MyChildView*)childView;
@end

@interface MyChildView : UIView
{
    id <MyChildViewDelegate> delegate;
    ...
}

@property (nonatomic, assign) id <MyChildViewDelegate> delegate;
...
@end

然后在你的视图中的某个地方你会要求字符串:

- (void)viewDidLoad
{
    ...
    NSArray* strings = [delegate getStringsForMyChildView:self];
    ...
}

然后在你的控制器(或任何地方)你可以这样做:

myChildView = [[MyChildView alloc] initWith....];
myChildView.delegate = self;

...

- (NSArray*)getStringsForMyChildView:(MyChildView*)childView
{
    return [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
}

在这种情况下可能有点矫枉过正,但这也是 UITableViews 的做法:它们有一个数据源委托向他们提供内容。

If you want to get really fancy, you can make a delegate for your child view.

@protocol MyChildViewDelegate
- (NSArray*)getStringsForMyChildView:(MyChildView*)childView;
@end

@interface MyChildView : UIView
{
    id <MyChildViewDelegate> delegate;
    ...
}

@property (nonatomic, assign) id <MyChildViewDelegate> delegate;
...
@end

Then somewhere in your view you would ask for the strings:

- (void)viewDidLoad
{
    ...
    NSArray* strings = [delegate getStringsForMyChildView:self];
    ...
}

Then in your controller (or where ever) you can do:

myChildView = [[MyChildView alloc] initWith....];
myChildView.delegate = self;

...

- (NSArray*)getStringsForMyChildView:(MyChildView*)childView
{
    return [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
}

It's probably a little overkill in this case, but this is how UITableViews do it too: they have a data source delegate to provide them with their contents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文