ios - 将数据传递给根 Controller

标签 ios objective-c uiviewcontroller

我正在开发一个联系人应用程序。我想通过打开这样的新 View 来添加联系人:

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

- (IBAction)addContact:(id)sender {

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

}

然后回来将联系人添加到 Root View Controller 的数组中:

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{

//创建联系人并将其放入 Root View Controller 的数组中

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

最佳答案

您应该使用委托(delegate)将新的 Contact 对象传达给您的 RootViewController。

定义协议(protocol)

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

在您的 AddContViewController 上有一个委托(delegate)属性:

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

在您的 addContact: 方法中分配委托(delegate):

- (IBAction)addContact:(id)sender {

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

}

在 RootViewController 中实现委托(delegate)方法:

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

从 AddContViewController 调用委托(delegate):

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

关于ios - 将数据传递给根 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12913565/

相关文章:

ios - 使用正则表达式提取两个标记之间的文本

android - RSA 解密跨 Android - iPhone

iphone - 自定义 UIView 子类作为 UIViewController 属性

ios - 使用自定义转换时如何处理呈现 UIViewController 中的方向更改

objective-c - 在 Objective-C 中获取方法的返回类型的方法?

iphone - 何时应该使用 initWithNibName 初始化 View Controller ?

iphone - 如何禁用 iOS 标签栏中的蓝色突出显示?

ios - Swift 绘图应用程序很慢

ios - 核心音频 : How to play MIDI MusicSequence by MusicPlayer while AUGraph is deprecated?

ios - 选择标签栏项目时显示模态视图