2012-10-16 63 views
0

我正在開發聯繫人應用程序。我要添加聯繫人,打開這樣一個新的觀點:將數據傳遞到根控制器

RootViewController.m ... 它有一個NSMutableArray的所謂接觸

- (IBAction)addContact:(id)sender { 

    AddContViewController *cont = [[AddContViewController alloc]init]; 
    [self.navigationController presentViewController:cont animated:YES completion:nil]; 

} 

,然後再回來,並添加聯繫人根視圖控制器的陣列:

AddContViewController.m

- (IBAction)acceptAction:(id)sender { 


    if ([[firstName text] length] < 1 && [[lastName text] length] < 1) 
    { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Oh no!" message:@"Invalid contact information!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [alert show]; 
    } 
    else{ 

//創建接觸,並把它的根視圖控制器的陣列中

Contact *cont = [[Contact alloc]initWithFirstName:[firstName text] lastName:[lastName text] andDOB:[dobPicker date]]; 

//現在我不知道該怎麼辦....

[self dismissViewControllerAnimated:YES completion:^{ 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success!" message:@"Contact added successfully!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [alert show]; 
     }]; 
    } 
} 

回答

1

有幾種方法將數據傳遞回來。我建議設置一個委託方法。 任何進口後,這對您的AddContViewController.h的頂部添加:

@class addContViewController 
@protocol addContViewControllerDelegate <NSObject> 
-(void)addContViewController:(addContViewController *)controller didAddContact:(Contact *)contact; 
@end 

和接口部分之後添加

@property (nonatomic, weak) id <addContViewControllerDelegate> delegate; 

然後在你的RootViewController的。小時協議添加到界面線<addContViewControllerDelegate>
現在,在您RootViewController.m方法addContact你推新的看法之前,添加:

cont.delegate = self; 

現在,在您AddContViewController.m,而不是解僱的觀點,稱:

[self.delegate addContViewController:self didAddContact:cont]; 

這將在您的RootViewController中調用一個新的方法,它將傳遞聯繫人,並且在這裏您可以使用它執行想要的操作,但首先關閉視圖:

-(void)addContViewController:(addContViewController *)controller didAddContact:(Contact *)contact { 
self dismissViewControllerAnimated:YES; 

} 
3

你應該使用代表新聯繫人對象傳回您的RootViewController。

定義協議

@protocol AddContDelegate 
    -(void)didAddnewContact:(Contact *)contact; 
@end 

在您AddContViewController有一個代表屬性:

@property (nonatomic, assign) id<AddContDelegate> delegate; 

在你的addContact:方法指定委託:

- (IBAction)addContact:(id)sender { 

    AddContViewController *cont = [[AddContViewController alloc]init]; 
    cont.delegate = self; 
    [self.navigationController presentViewController:cont animated:YES completion:nil]; 

} 

實現委託方法RootViewController:

-(void)didAddnewContact:(Contact *)contact { 
    [contacts addObject:contact]; 
} 

呼叫從AddContViewController委託:

- (IBAction)acceptAction:(id)sender { 


    if ([[firstName text] length] < 1 && [[lastName text] length] < 1) 
    { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Oh no!" message:@"Invalid contact information!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [alert show]; 
    } 
    else{ 

    Contact *cont = [[Contact alloc]initWithFirstName:[firstName text] lastName:[lastName text] andDOB:[dobPicker date]]; 

    if([self.delegate respondsToSelector:@selector(didAddnewContact:)]) { 
     [self.delegate didAddnewContact:cont]; 
    } 

    [self dismissViewControllerAnimated:YES completion:^{ 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success!" message:@"Contact added successfully!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [alert show]; 
     }]; 
    } 
} 
0

您可能需要使用代表這個動作,這樣就可以在RootViewController的溝通,有聯繫的對象。

使用導航視圖控制器堆棧使用[navigationViewController viewControllers]可以實現同樣的效果,並且最後一個對象屬於您的類的類型,您可以執行根的某個選擇器並使用成功消息解除您的AddContViewController

希望這將幫助!

相關問題