【问题标题】:Pass data back to Viewcontroller from tableview将数据从 tableview 传回 Viewcontroller
【发布时间】:2017-01-10 06:15:33
【问题描述】:

我有 ViewController 和 TableViewController。在 ViewController 我有 2 个按钮和 2 个文本字段,当单击 button1 时,它将导航到带有静态数据(如(Apple、Samsung、Blackberry、Windows)的 UITableView,单击 button2 时它将导航到 UITableView 静态数据(医生、工程师、商人,员工)当我选择任何一行时,它应该显示在 button1 的 textfield1 中单击,并且 button2 的 textField2 中单击了 ViewController。以下是我在学习时尝试过的,我不知道它是否正确。

CustomerVC.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
    - (IBAction)button1Click:(id)sender;
   @property (strong, nonatomic) IBOutlet UITextField *txtField1;

    - (IBAction)button2Click:(id)sender;
    @property (strong, nonatomic) IBOutlet UITextField *txtField2;



CustomerTableVC.h

#import <UIKit/UIKit.h>
    @interface DetailsViewController : UITableViewController
@end

CustomerTableVC.m

#import "DetailsViewController.h"
@interface DetailsViewController ()
{
    NSArray *array1,*array2;
}
@end

@implementation FamilyDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     array1=[[NSArray alloc]initWithObjects:@"Apple",@"Samsung",@"Blackberry",@"Windows", nil];

    array2=[[NSArray alloc]initWithObjects:@"Doctor",@"Engineer",@"Businessman",@"Employee", nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [array1 count];
    return [array2 count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[arrqy1 objectAtIndex:indexPath.row];
cell.textLabel.text=[arrqy2 objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.navigationController popViewControllerAnimated:YES];
}

【问题讨论】:

  • 创建 appdelegate 变量并存储每一行​​的值,而不是在文本字段中传递这些值
  • didSelectRowAtIndexPath 上,将数据保存在NSUserdefaults 中。在CustomerVCviewWillAppear 方法上,从Userdefaults 中获取数据并更新txtField1txtField1 值。
  • @prasad,你可以使用block传回数据,这是苹果推荐的方法。

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

你应该使用 Unwind segue 从你的 tableview 控制器传回数据..

要遵循的步骤:

假设 A 和 B 是两个控制器,您首先使用一些数据从 A 导航到 B。现在你想用一些数据从 B POP 到 A。

Unwind Segues 是最好的推荐方式。 步骤如下。

  1. 上午开放
  2. 定义以下方法

    @IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) {
    
    }
    
  3. 打开故事板

  4. 选择 B ViewController 并单击 ViewController 出口。按下控制键并拖动到“退出”出口并将鼠标留在此处。在下图中,选中的图标是 ViewController outlet,最后一个带有 Exit 标志的图标是 Exit Outlet。

  5. 您将在弹出窗口中看到“unwindSegueFromBtoA”方法。选择这个方法。

  6. 现在您将在左侧的视图控制器层次结构中看到一个 segue。您将在下图中的 StoryBoard Entry Piont 附近看到您创建的转场。

  1. 选择此项并为其设置标识符。 (建议设置和方法同名——unwindSegueFromBtoA)

  2. 打开 B.m 。现在,无论你想在哪里弹出到 A。使用

    self.performSegueWithIdentifier("unwindSegueFromBtoA", sender: dataToSend)
    
  3. 现在,当您弹出到“A”时,将调用“unwindSegueFromBtoA”方法。在“A”的 unwindSegueFromBtoA 中,您可以访问“B”的任何对象。

就是这样..!

【讨论】:

    【解决方案2】:

    尝试使用委托。 1. 在Tableview控制器上声明委托。

    自定义VC

    1. 声明FamilyViewController.delegate = self;
    2. self.view presentviewcontroller = familyviewController;
    3. 定义委托函数(例如detailsselected()
    4. detailsSelecteddismissViewController 内。

    FailyDetailsViewController

    1. didselectRowAtIndexPath : 内部调用“detailsSelected”函数并传递选定的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      相关资源
      最近更新 更多