我试图创建一个完全像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];
}
答案 0 :(得分:0)
您应该做的是在第一个视图控制器中创建委托协议,让第二个视图控制器实现协议方法,当您返回堆栈时,该协议方法会更新第一个视图控制器中的数据。
我制作了一个simple demonstration project,您可以下载并查看我的意思。
答案 1 :(得分:0)
您需要使用Core Data,它解决了通知或委派任何数据到super的问题。当您创建联系人时,包含所有联系人的视图控制器将自动更新。