我有2个UITable视图控制器链接,具有推送样式。第一个是静态表视图,当我点击它时将样式设置为右侧细节。它会将数据从细节传递到第二个UITable视图控制器以编辑值。一旦完成编辑。选中后退按钮,值将传回第一个UITable视图控制器。
点击第一个表视图控制器
时,我正在使用它 NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:myString forKey:@"amount"];
[myDefaults synchronize];
并从第二个UItable视图控制器上的(void)viewdidload中检索它
NSString *recoveredString = [[NSUserDefaults standardUserDefaults] objectForKey:@"amount"];
但是当我返回第一个表视图时它不起作用。它不会刷新第二个表视图中的新值传递。
我使用正确的方法在两个表视图之间传递数据吗?如果错了你的建议是什么?
答案 0 :(得分:0)
就个人而言,我使用通知来推送数据。我所做的是为两个UITableViews创建一个共享的单例类来访问。然后一个UITableView可以将通知发送到另一个视图,告诉它从单例类中读取。我总是喜欢听到其他方式。
答案 1 :(得分:0)
您需要使用委托从视图控制器传回值,无论它们是通过导航控制器推送还是以模态方式显示。类似的东西:
MyEditController.h:
@class MyEditController;
@protocol MyEditControllerDelegate <NSObject>
@optional
- (void)myEditController:(MyEditController *)myEditController
updatedThing:(Thing *)thing;
- (void)myEditController:(MyEditController *)myEditController
deletedThing:(Thing *)thing;
@end
@interface MyEditController : UITableViewController
{
id<MyEditControllerDelegate> _delegate;
Thing *_thing;
...
}
@property (assign, nonatomic, readwrite) id<MyEditControllerDelegate> delegate;
@property (retain, nonatomic, readwrite) Thing *thing;
...
@end
MyEditController.m:
...
- (void)updateThing
{
...
if ([_delegate respondsToSelector:@selector(myEditController:updatedThing:)])
{
[_delegate myEditController:self updatedThing:self.thing];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)deleteThing
{
...
if ([_delegate respondsToSelector:@selector(myEditController:deletedThing:)])
{
[_delegate myEditController:self deletedThing:self.thing];
}
[self.navigationController popViewControllerAnimated:YES];
}
...
@end
现在在父视图控制器中执行以下操作:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyEditController *myEditController = [[[MyEditController alloc] initWithNibName:@"EditView" bundle:nil] autorelease];
myEditController.delegate = self;
myEditController.thing = [self.thingList objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:myEditController animated:YES];
}
- (void)myEditController:(MyEditController *)myEditController
updatedThing:(Thing *)thing
{
// Update thing in self.thingList
// Reload table view row
}
- (void)myEditController:(MyEditController *)myEditController
deletedThing:(Thing *)thing
{
// Delete thing from self.thingList
// Delete table view row
}