Thursday, 25 August 2016

json parsing in iOS with table view

USE CASE - 1
        |
Main
        |
UITable View Controller
        |
UITable View
        |
UITable View Cell
       |
Components

1) Create UITable View
2) Add below Code in "MyTableViewController.h"

#import <UIKit/UIKit.h>

@interface MyTableViewController : UITableViewController
{
    NSMutableArray *arr1 ;
    NSMutableArray *arr2;
}

@end


3) Add below Code in "MyTableViewController.m"


- (void)viewDidLoad
{
   
    arr1=[[NSMutableArray alloc]init];
    
    arr2=[[NSMutableArray alloc]init];
    NSString *strUrl = @"http://www.jsonurl";
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
    
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueuecompletionHandler:^(NSURLResponse*response, NSData *data, NSError *error)
     {
         if(data != nil)
         {
             NSMutableArray *dictionaryObj = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:&error];
             
             
             //            NSArray *allValues = [dictsongs allValues];
             //             NSMutableArray *arrKeys = [[NSMutableArray alloc]initWithObjects:@"image",@"title",@"song_url", nil];
             
             for (int i = 0; i<dictionaryObj.count; i++) {
                 
                 
                 [arr1 addObject:[[dictionaryObjobjectAtIndex:i]objectForKey:@"Title"]];
                 [arr2 addObject:[[dictionaryObjobjectAtIndex:i]objectForKey:@"MedImageUrl"]];
                 
                 
                 NSLog(@"array at %@",arr1[i]);
                 
             }
             NSLog(@"images list %@",arr1);
             [self.tableView reloadData];
         }
         
         else
         {
             UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"" message:@"Server Down.Try again later"delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nilnil];
             [alert show];
             
             
         }
     }];
    
    
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return arr1.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifierforIndexPath:indexPath];
    
    cell.textLabel.text=[arr1 objectAtIndex:indexPath.row];
    
    NSData  *imgData=[NSData dataWithContentsOfURL:[NSURLURLWithString:[arr2 objectAtIndex:indexPath.row]]];
    cell.imageView.image=[UIImage imageWithData:imgData];
    
    return cell;
}



Tuesday, 23 August 2016

Search Bar - in iOS - Objective C

1) Design the viewcontroller with "searchbar", "tableview" and "tableviewcell" like below



2) drag & drop "datasource" to viewcontroller


3) Give CellID as identifier

4) drag & drop "searchobj" to view controller
5) drag & drop "tblobj" to viewcontroller
6)


Friday, 24 June 2016

UIModelTransition in iOS - Objective C

1) Create single view application
2) Create FirstViewController and Secod View Controller classes
3) Design like following

4) Select first button, drag and drop to firstviewcontroller

5) Add class name to firstview controller  and second view controller

6) Select Segue change transition style to FlipHorizontal

7) Run the program

UIModelTransition in iOS - Objective C

1) Create single view application
2) Create FirstViewController and Secod View Controller classes
3) Design like following

4) Select first button, drag and drop to firstviewcontroller

5) Add class name to firstview controller  and second view controller

6) Select Segue change transition style to FlipHorizontal

7) Run the program

Custom Segue for UIViewAnimation in iOS - Objective C

1) Create a single view application
2) Select Main.StoryBoard
3) Design the interface by following



4) Click on "Go Next Button" -->Control drag towards Detail View -->Select Seque Name "Custom"

5) Create a new class named as "mysegue" which is going to be subclass of UIStoryBoardSegue
6) Go to StoryBoard select segue -->Open inspector select the class name "my segue"

7) Go to my segue class and implement a method called perform, for implementing custom animation with UIViewanimation

8) Reuse my segue class in application any where

Monday, 20 June 2016

Collection View Controller in iOS - Objective C

1) Create single view application
2) Select Story Board, delete existing view controller
3) Drag & Drop UICollectionViewController in to StoryBoard
4) Delete existing viewcontroller.h and viewcontroller.m
5) Add NewFile --> Enter new class name as mycollectionviewcontroller.h--> subclass of  UICollectionViewController
6) In Main.Storyboard add custom class

7) Select UICollectionview cell -->enter identifier as "Cell"

8) Drag & Drop Image view on UICollectionviewcell -->change Tag Value to "100"


9) Drag & drop Label on UICollectionview cell -->change Tag Value to "101"

10) Add below code in Mycollectionviewcontroller.h

@interface MyCollectionViewController : UICollectionViewController
{
    NSArray *devicePhotos;
    NSArray *deviceNameArr;
}

@end



11) Add below code in my controller.m
       1) Add below code in viewdidload

 devicePhotos=[[NSArray alloc]initWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg",@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg",@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg",nil];
    deviceNameArr=[[NSArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iPadmini", @"iPhone",@"iPad",@"iPod",@"iPadmini", @"iPhone",@"iPad",@"iPod",@"iPadmini", nil];


12) Add below methods in view controller.m

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return devicePhotos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    // product Image
    UIImageView *iv = (UIImageView *)[cell viewWithTag:100];
    iv.image = [UIImage imageNamed:[devicePhotos objectAtIndex:indexPath.row]];
    // Product Name
    UILabel *lbl=(UILabel *)[cell viewWithTag:101];
    lbl.text=[deviceNameArr objectAtIndex:indexPath.row];
    
    return cell;

}

13) Build & Run the project

Reference: http://www.appcoda.com/ios-programming-uicollectionview-tutorial



Thursday, 16 June 2016

Navigation Controller in iOS - Objective C

1) create single view application
2) click on main.storyboard --> select view controller
3) go to Editor in menu --> EmbedIn --> navigation controller
4) select navigation controller scene, select navigator bar --> change attributes with customization like tint color,bar tint color,title color
5) go to main controller --> drag & drop two buttons
    1) Go to first view button
    2) Go to second view button
6) drag & drop two view controllers in to story board scene
7) Select Main view --> select first button -->control drag towards first controller--> connect segue name as show
8) do the same process for second view controller also
9) Add two UI View Controller subclasses in navigator window and configure them properly with view controller scenes
                                     How to configure 9th step
Select view controller, open identity inspector, Select drop down for the class name--->choose class