【问题标题】:How to pass data from modal view controller to parent view如何将数据从模态视图控制器传递到父视图
【发布时间】:2012-10-23 15:22:16
【问题描述】:

我正在为看似非常简单的事情而苦苦挣扎:将数据从模态视图传递到其父级。我尝试了无数种方法,令我惊讶的是,网上没有太多东西,似乎没有什么能与我想做的事情相匹配。我正在使用故事板,并且大多数示例不使用序列/故事板。

如果有人可以为我提供一些示例代码,我将不胜感激!

我有一个带有自定义单元格的静态表格视图控制器。在单元格内,我有一个标签。我想点击单元格以呈现带有文本视图的模态视图。在文本视图中输入数据,然后点击保存/完成按钮以关闭模式视图并使该数据显示在单元格中的 UILabel 上。

我知道这听起来一定很简单,这里有很多关于它的问题,但没有什么能真正做到这一点。一些示例代码将不胜感激。我完全被困在构建我的应用程序上,在我得到这个之前不能再进一步了!

【问题讨论】:

    标签: storyboard modalviewcontroller uistoryboardsegue pass-data


    【解决方案1】:

    您可以尝试 NSUserDefaults 从 textview 存储您的数据,然后将它们读回标签。

    编辑:如果您不想使用 NSUserDefaults,因为它不是“正确”的方式(但很简单),您可以试试这个:

    在你的 tableViewController.h 中创建一个 NSString:

    #import <UIKit/UIKit.h>
    @interface TestTableViewController : UITableViewController
    @property (nonatomic, strong) NSString *string;
    @end
    

    在包含 textView 的 viewController.h 中添加以下内容:

    - (IBAction)doneButton;
    @property (weak, nonatomic) IBOutlet UITextView *textView;
    

    在 viewController.m 中:

    - (IBAction)doneButton {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    TestTableViewController *tvc = (TestTableViewController*)[storyboard instantiateViewControllerWithIdentifier:@"TestTableViewController"];
    tvc.string = _textView.text;
    [tvc setModalPresentationStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:tvc animated:YES];
    // ios6 version:
    //[self presentViewController:tvc animated:YES completion:nil];}
    

    然后在你的 tableViewController.m 中显示输入数据到你的单元格(你不需要使用标签):

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {      
    static NSString *CellIdentifier = @"test";
    UITableViewCell *cell = [[UITableViewCell alloc] init]; 
    if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; } 
    else { 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    } 
     // Configure the cell...
    cell.textLabel.text = _string;
    
    return cell;}
    

    不要忘记在你的 tableViewController 的 Storyboard 上将 Storyboard ID 设置为身份检查器!!

    【讨论】:

    • 另外我不确定,但由于我使用的是 Xcode 4.2,我想我需要一个 segue ID?另外,我收到一个错误:自动引用计数问题:选择器“dequeueReusableCellWithIdentifier:forIndexPath:”没有已知的类方法
    • 该错误是因为您正在为 ios6 之前的 ios 版本编码。添加这个: UITableViewCell *cell = [[UITableViewCell alloc] init]; if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; } else { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];并改变 [self presentViewController:tvc animated:YES completion:nil];对此:[self presentModalViewController:tvc animated:YES];
    • Acually,我很抱歉,但如果您不介意编辑上面的代码,我将不胜感激。我收到您评论中的代码的各种错误。非常感谢!
    • 哦哦对不起!!!我只是看到您使用的是 xcode 4.2,所以只需删除 ios 6 if 语句!否则会报错!保持这个: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    • 好的,现在一切正常...没有错误。但我认为我仍然缺少segue或其他东西?我不确定你的意思是什么?不要忘记为您的 tableViewController 设置 Storyboard ID 为 Storyboard 上的身份检查器!!
    猜你喜欢
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2017-03-14
    • 2017-01-10
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多