iphone - 使用模态视图 Controller 添加到 TableView

标签 iphone objective-c ios uitableview modalviewcontroller

在 Internet 上的任何地方,我都能找到有关如何通过使用带有绿色加号的特殊行“添加行”来将行添加到 TableView 中的示例。但我不想那样。

我想在 MyTableViewController 的标题栏中有一个加号按钮,并使用 XIB 文件调用一些模态添加 View Controller ,只有一个文本字段来填充它。在这个模态添加 View Controller 中,我想填写这个文本字段,在我按下完成后,模态添加 View Controller 消失,我想找到添加到 MyTableViewController TableView 的文本。

我的 MyTableViewController 中有一个属性来保存它的所有行:

@property (nonatomic, retain) NSMutableArray *list;;

我就是无法添加行来工作。我不知道我可以在哪里做

[list addObject:];

这是我在用户按下标题栏中的加号按钮时调用的 MyTableViewController addItem 方法的代码:

- (IBAction) addItem: (id) sender;
{
    NSLog(@"Adding item...");
    //Preparing "Add View" which has a single text field
    AddViewController *addViewController = [[AddViewController alloc] init];
    addViewController.title = @"Add Item";

    UINavigationController *modalController = [[UINavigationController alloc]
    initWithRootViewController:addViewController];
    [addViewController release];

    // Showing the prepared Add View controller modally
    [self.navigationController presentModalViewController:modalController animated:YES]
    NSLog(@"Modal controller has been presented.");

    [modalController release];
}

下面是 AddViewController 中的代码,它在输入文本字段并在标题栏中按完成后调用:

- (IBAction) done: (id) sender
{
    NSLog(@"Reached Done");
    if (textField != nil) {
        self.fieldText = textField.text;
    }
    NSLog(@"About to dissmiss modal controller...");
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
    NSLog(@"Modal controller has been dismissed."); 
}

最佳答案

为此类添加 Controller 创建委托(delegate)协议(protocol)并使父 Controller 成为其委托(delegate)是很常见的。

当添加 Controller “完成”(即未使用可能的取消按钮取消)时,它会调用委托(delegate)方法,例如 addControllerIsDone: 让父 TableView Controller 知道它应该获取设置值,将其添加到列表中,然后关闭添加 Controller 。

您还可以将列表传递给添加 Controller ,让它在 [parentViewController dismissModalViewControllerAnimated:YES] 调用之前自行添加设置值。

这取决于您是想在 TableView Controller 中保留列表的控制权,还是想将其传递给添加 Controller 。

在关闭 Add Controller 之后,您可以找出应该在 tableView 中添加新条目的单元格的位置,并使用漂亮的动画插入它,重新加载该部分(也可以使用动画)或整个 tableView(不使用动画)可能)。


第一个选项可能是这样的:

@class AddViewController;

@protocol AddViewControllerDelegate <NSObject>
- (void)controllerIsDone:(AddViewController *)controller;
@end

@interface AddViewController : UIViewController
@property (nonatomic, assign) id<AddViewControllerDelegate> delegate;
@end

和“完成”代码

- (IBAction) done: (id) sender
{
    ......
    [self.delegate controllerIsDone:self];
    NSLog(@"About to dissmiss modal controller...");
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
    NSLog(@"Modal controller has been dismissed."); 
}

还有 MyViewController:

@interface MyViewController : UIViewController <AddViewControllerDelegate>
@end

所以它必须实现 controllerIsDone: 方法。例如:

- (void)controllerIsDone:(AddViewController *)controller
{
    [self.list addObject:controller.textField.text];
}

由于 AddViewController 自行关闭,MyViewController 不必在委托(delegate)方法中执行此操作。但好的做法是,如果你弹出模态视图 Controller ,你也应该关闭它,只是为了对称。 ;)

在这种情况下,当然 textField 必须是可公开访问的属性。

我相信您会想出第二​​个选项。

继续阅读 Decorator pattern in Cocoa Fundamentals Guide .

关于iphone - 使用模态视图 Controller 添加到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6237705/

相关文章:

ios - FFMpeg avformat_write_header 总是返回 < 0

iphone - 如何获取动画结束通知

objective-c - 不要播放声音

ios - 添加到 NSMutableDictionary 而不替换相似的键

iphone - 检查 NSURLRequest 目标是否为 "_blank"?

ios - 在 iOS 中设置模糊的图像背景

ios - 如何在没有响应描述映射的情况下使用 RestKit?

iphone - 调用 Popover 时使主视图变暗

ios - CoreBluetooth 函数不适用于 Singleton

iOS - 如何将事件添加到 Objective C 中的 kal 库?