我正在尝试创建一个与 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];
}