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

我的第三个观点:

-(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

您应该做的是在您的第一个视图控制器中创建一个委托协议,并让您的第二个视图控制器实现协议方法,当您返回堆栈时更新第一个视图控制器中的数据。

我做了一个简单的演示项目,你可以下载并看看我的意思。

于 2012-09-01T11:19:42.290 回答
0

您需要使用 Core Data,它解决了将任何数据通知或委托给 super 的问题。创建联系人后,包含所有联系人的视图控制器将自动更新。

于 2012-09-01T13:34:10.460 回答