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

3 に答える 3

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];
        }];
    }
}
于 2012-10-16T11:31:49.637 に答える
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 で新しいメソッドが呼び出され、Contact が渡されます。ここでは、必要に応じて操作できますが、最初にビューを閉じます。

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

}
于 2012-10-16T11:44:26.413 に答える
0

rootviewcontroller と通信して連絡先オブジェクトを取得できるように、このアクションにデリゲートを使用することができます。

[navigationViewController viewControllers]最後のオブジェクトは、ルートの特定のセレクターを実行AddContViewControllerし、成功メッセージで閉じることができるクラスのタイプです。

これが役立つことを願っています!!

于 2012-10-16T11:46:07.017 に答える