Reputation: 59
I have ViewController and TableViewController. In ViewController i have a 2 buttons and 2 textfields, when button1 is clicked it will navigate to UITableView with static data like (Apple, Samsung, Blackberry, Windows),and button2 is clicked it will navigate to UITableView static data (Doctor, Engineer,Businessman, Employee)when i select any of the row it should be displayed in the textfield1 of button1 clicked and textField2 of button2 clicked ViewController. below is what i have tried as am learning, i don't know wheather it is correct or not.
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];
}
Upvotes: 0
Views: 783
Reputation: 113
Try using delegate.
1. Declare delegate on Tableview
controller's.
In Custom Vc
FamilyViewController.delegate = self;
self.view presentviewcontroller = familyviewController;
detailsselected()
)detailsSelected
dismissViewController
. In FailyDetailsViewController
:
didselectRowAtIndexPath :
call the "detailsSelected
" function and pass the selected values.Upvotes: 0
Reputation: 997
You should use Unwind segue to pass data back from your tableview controller..
Steps to follow:
Suppose A & B are two controllers and you first navigated from A to B with some data. And now you want to POP from B to A with some data.
Unwind Segues is the best and recommended way to do this. Here are the steps.
define following method
@IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) {
}
open storyboard
Select B ViewController and click on ViewController outlet. press control key and drag to 'Exit' outlet and leave mouse here. In below image, selected icon is ViewController outlet and the last one with Exit sign is Exit Outlet.
You will see 'unwindSegueFromBtoA' method in a popup . Select this method .
Now you will see a segue in your view controler hierarchy in left side. You will see your created segue near StoryBoard Entry Piont in following Image.
Select this and set an identifier to it. (suggest to set the same name as method - unwindSegueFromBtoA)
Open B.m . Now, wherever you want to pop to A. use
self.performSegueWithIdentifier("unwindSegueFromBtoA", sender: dataToSend)
Now when you will pop to 'A', 'unwindSegueFromBtoA' method will be called. In unwindSegueFromBtoA of 'A' you can access any object of 'B'.
That's it..!
Upvotes: 1