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:-
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:-
You can also download the application from the link given below:-
0 Comment(s)