2010-11-06 26 views
0

我正在構建一個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 [email protected]"%i",[DPView Getrow]; 
} 

這是pickerview的代碼的按鈕:

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

回答

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]; 
} 
+0

感謝,這讓有點感覺,但我現在得到一個錯誤。在這一行上:pView.parent = self;我得到:要求成員父母的東西不是一個結構或工會 – ULcajun 2010-11-06 17:52:08

+0

沒關係,我明白了。主要觀點與孩子不同,但我明白了。我得到一個錯誤壽,不知道它是否會令人煩惱。 dpView.parent = self。自我是類WoWViewController。 dpView是DayPickerViewController類。我從錯誤的Objective-C類型中傳遞'setParent:'的參數1時出現錯誤:警告:不兼容的Objective-C類型的結構體WoWCalculatorViewController *',預期的'struct DayPickerViewController *' – ULcajun 2010-11-06 18:04:11

+0

對不起,這是我的錯誤。我編輯了我的答案,並將父項屬性移至正確的類,並將其設置爲正確的類型。 – Codo 2010-11-06 18:20:23

相關問題