繁体   English   中英

将数据从tableview传递回Viewcontroller

[英]Pass data back to Viewcontroller from tableview

我有ViewController和TableViewController。 在ViewController中,我有2个按钮和2个文本字段,当单击button1时,它将导航到带有静态数据(如Apple,三星,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];
}

您应该使用Unwind segue将数据从tableview控制器传回。

遵循的步骤:

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

放松Segues是执行此操作的最佳建议。 步骤如下。

  1. 营业时间
  2. 定义以下方法

     @IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) { } 
  3. 打开情节提要

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

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

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

您查看层次结构

  1. 选择它并为其设置标识符。 (建议设置与方法相同的名称-unwindSegueFromBtoA)

  2. 打开Bm。 现在,无论您要弹出A的哪个位置,都可以使用

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

而已..!

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

自订Vc中

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

FailyDetailsViewController

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM