2012-10-16 66 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

希望这将帮助!

相关问题