将数据传递给根控制器

时间:2012-10-16 11:20:40

标签: ios objective-c uiviewcontroller

我正在开发一个联系人应用。我想添加一个联系人,打开一个像这样的新视图:

RootViewController.m ...  它有一个名为contacts

的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];
        }];
    }
}

3 个答案:

答案 0 :(得分:3)

您应该使用委托 新的Contact对象传递回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];
        }];
    }
}

答案 1 :(得分:1)

有几种方法可以传回数据。我建议设置一个委托方法。 在任何导入之后将它添加到AddContViewController.h的顶部:

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

在界面部分添加

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

然后在RootViewController.h中将协议添加到接口行<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;

}

答案 2 :(得分:0)

您可能希望使用委托进行此操作,以便您可以与rootviewcontroller进行通信以获得联系对象。

使用[navigationViewController viewControllers]的navigationViewController堆栈可以实现同样的效果,并且最后一个对象属于您的类的类型,您可以执行根的某个选择器并使用成功消息解除您的AddContViewController

希望这会有所帮助!!