-2

iPhone とまったく同じ連絡先アプリケーションを作成しようとしています。クリックすると、FirstViewに追加ボタンがあり、クリックすると「NewContact」ビューにモーダルが表示され、plistが作成され、すべての値がplistに書き込まれ、ビューが閉じてfirstViewに戻ります。firstView で名前を表示するのが難しいと感じています。

私の最初の見解:

-(void)btnrightClicked:(id)sender
{
    NewContactViewController *newContact= [[NewContactViewController alloc]initWithNibName:@"NewContactViewController" bundle:nil];
    [self presentModalViewController:newContact animated:YES];  
}

私の3番目の見解:

-(IBAction)btnDoneClicked:(id) sender
{       
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Details.plist"];

    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL success = [fm fileExistsAtPath:filePath];
    if(!success)
    {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"Details" ofType:@"plist"];
        [fm copyItemAtPath:path toPath:filePath error:nil];
    }

    NSMutableArray *contacts = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSMutableDictionary *dict = [NSMutableDictionary new];

if ([txtfirst.text length] > 0) 
{
    [dict setObject:txtfirst.text forKey:@"first"];
}
else {
    alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Dont leave name blank" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}



if ([txtlast.text length] > 0) 
{
    [dict setObject:txtlast.text forKey:@"last"];
}


if ([txtwork.text length] > 0) 
{
    [dict setObject:txtwork.text forKey:@"work"];
}


if ([txtnumber1.text length] > 0) 
{
    [dict setObject:txtnumber1.text forKey:@"number1"];
}
else {
    alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Dont leave number blank" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}


if ([txtnumber2.text length] > 0) 
{
    [dict setObject:txtnumber2.text forKey:@"number2"];
}

if ([txtringtone.text length] > 0) 
{
    [dict setObject:txtringtone.text forKey:@"ringtone"];
}

if ([txtemailid1.text length] > 0) 
{
    [dict setObject:txtemailid1.text forKey:@"emailid1"];
}

if ([txtemailid2.text length] > 0) 
{
    [dict setObject:txtemailid2.text forKey:@"emailid2"];
}



[contacts addObject:dict];
[contacts writeToFile:filePath atomically:YES];
[dict release];

{
    if ([txtfirst.text length] > 0 && [txtnumber1.text length] > 0) {
        alertView = [[UIAlertView alloc] initWithTitle:(txtfirst.text) message:@"Your details saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
}
    [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

}
4

2 に答える 2

0

スーパーにデータを通知または委任する問題を解決する Core Data を使用する必要があります。連絡先を作成すると、すべての連絡先を含むビュー コントローラーが自動的に更新されます。

于 2012-09-01T13:34:10.460 に答える
0

あなたがしなければならないことは、最初のビューコントローラーでデリゲートプロトコルを作成し、2番目のビューコントローラーに、スタックを下に戻ったときに最初のビューコントローラーのデータを更新するプロトコルメソッドを実装させることです。

簡単なデモンストレーション プロジェクトを作成しました。このプロジェクトをダウンロードして見て、私の言いたいことを確認してください。

于 2012-09-01T11:19:42.290 に答える