2017-01-10 78 views
0

我有ViewController和TableViewController。在ViewController中,我有2个按钮和2个文本框,点击button1时,它将导航到带有静态数据(如Apple,Samsung,Blackberry,Windows)的UITableView,并且button2被点击,它将导航到UITableView静态数据(Doctor,商人,员工),当我选择任何行应该显示在button1的textfield1点击和button2的textField2点击ViewController。下面是我试着学习的东西,我不知道它是否正确。从tableview将数据传回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]; 
} 
+1

创建的appdelegate变量和存储在比在文本字段 – iOS

+0

传递这些值在'didSelectRowAtIndexPath'每一行值,在保存'NSUserdefaults'的数据。在'CustomerVC'的'viewWillAppear'方法中,从Userdefaults获取数据并更新'txtField1'和'txtField1'Vlaues。 – pkc456

+1

@prasad,你可以使用block传回数据,这是苹果推荐的方法。 – aircraft

回答

0

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

定制Vc的

  1. 声明FamilyViewController.delegate = self;
  2. self.view presentviewcontroller = familyviewController;
  3. 定义委托功能(例如detailsselected()
  4. 里面的detailsSelecteddismissViewController

FailyDetailsViewController

  1. 里面didselectRowAtIndexPath :呼叫的 “detailsSelected” 功能,并通过所选择的值。
1

您应该使用开卷SEGUE来传递数据从你的tableview控制器回..

遵循的步骤:

假设A & B两个控制器和您第一次从一个导航到B与一些数据。而现在你想用一些数据从B到A的POP。

Unwind Segues是最好的和推荐的方式来做到这一点。 以下是步骤。

  1. 打开A.M
  2. 定义下面的方法

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

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

  5. 您会在弹出窗口中看到'unwindSegueFromBtoA'方法。选择此方法。

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

You View Hierarchy

  • 选择此和一个标识符设置为它。 (建议设置与方法 - unwindSegueFromBtoA相同的名称)

  • Open B.m。现在,无论你想弹出到A.使用现在

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

  • 就是这样..!

    相关问题