Hi Reader's,
This blog includes the concept of TabBarController and how you can transfer data from one controller to another. TabBarController is use to manage different content view controller which you provides according to your need. Here given below is an example using the concept of TabBarController and transfer data from one controller to another. Snapshot of storyboard is as follows:-
![](https://lh6.googleusercontent.com/-v027Giq8YaM/V3OUtipkx9I/AAAAAAAAF-Q/u7VgCvxKcpc2Xp6BzW5CvgWz4L27cRnUACL0B/w767-h624-no/Screen%2BShot%2B2016-06-29%2Bat%2B2.30.45%2BPM.png)
![](https://lh6.googleusercontent.com/-At6KOS4nnlo/V3OUto-LWVI/AAAAAAAAF-Q/0CJ2sdJd3_Q8BzHUkP1ZRJ149WNAt_2agCL0B/w1278-h554-no/Screen%2BShot%2B2016-06-29%2Bat%2B2.35.24%2BPM.png)
Now the coding part of the application is given below:-
First of all login page coding is given below which take username and password for further processing.
LoginViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#import "TabApplicationViewController.h"
#import "FirstViewController.h"
@interface LoginViewController : UIViewController
- (IBAction)btnLogin:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *txtUserName;
@property (weak, nonatomic) IBOutlet UITextField *txtPassword;
@end
LoginViewController.m
#import "LoginViewController.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnLogin:(id)sender {
if(_txtUserName.text.length>0 && _txtPassword.text.length>0)
{
[self performSegueWithIdentifier:@"tabController" sender:nil];
}
else{
NSString *message=[NSString stringWithFormat:@"%@",@"Invalid username/password"];
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Login" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *dismissAction=[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnullaction) {
}];
[alert addAction:dismissAction];
[self presentViewController:alert animated:YES completion:nil];
}
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"tabController"])
{
TabApplicationViewController *tab=[segue destinationViewController];
UINavigationController *nav=[[tab viewControllers]firstObject];
FirstViewController *home=[[nav viewControllers]firstObject];
[home setUserName:_txtUserName.text];
UINavigationController *nav1=[[tab viewControllers]lastObject];
SecondViewController *home1=[[nav1 viewControllers]firstObject];
[home1 setUserName:_txtUserName.text];
}
}
@end
SubTableViewCell.h
#import <UIKit/UIKit.h>
@interface SubTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *lblName;
@property (weak, nonatomic) NSIndexPath *currentIndexPath;
@end
SubTableViewCell.m
#import "SubTableViewCell.h"
@implementation SubTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
Below is the coding for home page after login which includes a list of names in the form of a table view.
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SubTableViewCell.h"
#import "DataViewController.h"
@interface FirstViewController : UIViewController
@property(nonatomic,strong)NSString *userName;
@end
FirstViewController.m
#import "FirstViewController.h"
#import "TabApplicationViewController.h"
@interface FirstViewController ()
{
NSArray *arrofNames;
NSString *data;
SubTableViewCell *cell;
}
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
arrofNames = [[NSArray alloc]initWithObjects:@"Tanuja",@"Sukanya",@"Jyoti", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arrofNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cell1";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.currentIndexPath = indexPath;
[cell.lblName setText:[arrofNames objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
data=[arrofNames objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"dataController" sender:nil];
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"dataController"])
{
DataViewController *home=[segue destinationViewController];
[home setName:data];
[home setUserName:_userName];
}
}
@end
If you will click on any name given in the table view list you will get an another page with the name of user and name written on the list. Below code will include code for this purpose:-
DataViewController.h
#import <UIKit/UIKit.h>
@interface DataViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *lblUserName;
@property (weak, nonatomic) IBOutlet UILabel *lblData;
@property(strong,nonatomic)NSString *name;
@property(strong,nonatomic)NSString *userName;
@end
DataViewController.m
#import "DataViewController.h"
@interface DataViewController ()
@end
@implementation DataViewController
- (void)viewDidLoad {
[super viewDidLoad];
_lblData.text=_name;
_lblUserName.text=_userName;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Given below is the code for profile page which includes a button and after button click you will get the username again which we are passing from one controller to another.
SecondViewController.h
#import <UIKit/UIKit.h>
#import "SecondDataViewController.h"
@interface SecondViewController : UIViewController
@property(strong,nonatomic)NSString *userName;
- (IBAction)secondBtn:(id)sender;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)secondBtn:(id)sender {
[self performSegueWithIdentifier:@"secondNavigation" sender:nil];
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"secondNavigation"])
{
SecondDataViewController *home=[segue destinationViewController];
[home setUser:_userName];
}
}
@end
Now below is the code given to get the profile data on button click by a user in SecondViewController.
SecondDataViewController.h
#import <UIKit/UIKit.h>
@interface SecondDataViewController : UIViewController
@property(strong,nonatomic)NSString *user;
@property (weak, nonatomic) IBOutlet UILabel *lblData;
@end
SecondDataViewController.m
#import "SecondDataViewController.h"
@interface SecondDataViewController ()
@end
@implementation SecondDataViewController
- (void)viewDidLoad {
[super viewDidLoad];
_lblData.text=_user;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Output:-
Credentials to login in the application are:-
Username=tanuja
Password=tanuja
Login screen will be visible in the given way:-
![](https://lh3.googleusercontent.com/-oQWTh3Xylxc/V3OUtrxTbnI/AAAAAAAAF-Q/oGPkVUnMj9sks7PRSWeXXU4BFmfUMDR4gCL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B29%252C%2B2016%252C%2B2.36.03%2BPM.png)
![](https://lh3.googleusercontent.com/-KtKOvro9TZY/V3OUuCUd5kI/AAAAAAAAF-Q/KKeL9F9MGYUlU_a6ZQWA6a8vK9bQWy0CACL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B29%252C%2B2016%252C%2B2.36.19%2BPM.png)
![](https://lh3.googleusercontent.com/-NDAudUY0Yn0/V3OUuGy9t0I/AAAAAAAAF-Q/GmORk-mya04lF0uFOC6_D4EgYqLzHU5dQCL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B29%252C%2B2016%252C%2B2.36.24%2BPM.png)
![](https://lh3.googleusercontent.com/-Rnp0rJah1Hg/V3O-KzFDKAI/AAAAAAAAF-s/raM7_OCVBjExs7fFx6ES4AbrLxIZr9zggCL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B29%252C%2B2016%252C%2B5.54.27%2BPM.png)
![](https://lh3.googleusercontent.com/-XaTJHdXwLPM/V3OUujbsZsI/AAAAAAAAF-Q/sbobkta4JBgDwMsL0nUBh-COcjC4QTOhACL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B29%252C%2B2016%252C%2B2.36.36%2BPM.png)
![](https://lh3.googleusercontent.com/-ZKMyMiPLepU/V3OUu3PPUOI/AAAAAAAAF-Q/ZJ6AfNgxlVQh-FUsecD44IQE4Pz16w9yQCL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B29%252C%2B2016%252C%2B2.36.42%2BPM.png)
You can also download the application from the link given below:-
0 Comment(s)