2017-01-10 74 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'的任何對象。

  • 就是這樣..!

    相關問題