2

我有 2 个带有推送样式的 UITable 视图控制器链接。第一个是静态表格视图,当我点击它时将样式设置为正确的细节。它将数据从详细信息传递到第二个 UITable 视图控制器以编辑值。一旦完成编辑。选项卡返回按钮,值将传递回第一个 UITable 视图控制器。

我在点击第一个表视图控制器时使用它

 NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:myString forKey:@"amount"];
[myDefaults synchronize];

并从第二个 UItable 视图控制器上的 (void)viewdidload 中检索它

NSString *recoveredString = [[NSUserDefaults standardUserDefaults] objectForKey:@"amount"];

但是当我返回第一个表格视图时它不起作用。它不会使用来自第二个表视图的新值传递来刷新。

我是否使用正确的方法在两个表视图之间传递数据?如果错了,你的建议是什么?

4

2 回答 2

0

就个人而言,我使用通知来推送数据。我所做的是为两个 UITableViews 创建一个共享的单例类来访问。然后一个 UITableView 可以向另一个视图发送通知,告诉它从单例类中读取。不过,我总是喜欢听其他方法。

于 2012-08-20T12:44:50.620 回答
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
}
于 2012-08-20T13:17:48.990 回答