如何在视图完成并返回时发出信号?

时间:2010-11-06 17:14:43

标签: objective-c xcode

我正在构建一个iphone应用程序,当按下按钮时,我的第一个视图会调用第二个视图。第二个视图是带按钮的UIPicker。按下按钮时,我想从UIPicker返回数据。我该怎么办?这是我到目前为止的代码:

这是调用子视图的按钮

-(IBAction)DayView:(id)sender{
 DayPickerViewController *DPView =[[DayPickerViewController alloc]
           initWithNibName:nil bundle:nil];
 [self presentModalViewController:DPView animated:YES];
    //tried below, doesnt work
 //[calc SetDay:[DPView Getrow]];
 //DaysButton.titleLabel.text =@"%i",[DPView Getrow]; 
}

这是pickerview的代码:

-(IBAction)buttonPressed{
 NSInteger row = [dayPicker selectedRowInComponent:0];
 data = row;
 [self dismissModalViewControllerAnimated:YES];
}

1 个答案:

答案 0 :(得分:0)

您尝试的方法首先在Windows上运行,您可以在其中以与输入和离开函数类似的方式打开和关闭模式对话框。但是,iOS不能像这样工作。

最简单的方法是在第二个视图中添加一个引用父视图的实例变量。然后,您可以通过直接调用实例方法来通知父视图:

主要观点:

-(IBAction) dayView:(id)sender {
    DayPickerViewController *dpView = [[DayPickerViewController alloc]
        initWithNibName:nil bundle:nil];
    dpView.parent = self;
    [self presentModalViewController:dpView animated:YES];
}

-(void) viewController:(DayPickerViewController*)controller
          didSelectRow:(NSInteger)row {
    // the child view has been dismissed,
    // do something with the selected row...
}

儿童观点:

@property(nonatomic, retain) WoWViewController* parent;

...

@synthesize parent;

-(IBAction) buttonPressed {
    NSInteger row = [dayPicker selectedRowInComponent:0];
    [self dismissModalViewControllerAnimated:YES];
    [self.parent viewController:self didSelectRow:row];
}