Wednesday, 19 June 2013

Table View With Section (Grouped Table View)

For grouped tableview fallow the following steps...

1. Create new Project.
2. Drag a uitableview from object library.
3. Change the style of uitableview from plain(default) to grouped from selecting the attribute inspector..
4. Make the chenges in .h file


@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray* arrPhysics;
    NSMutableArray* arrMathematics;
    NSMutableArray* arrHeader;
}
@property (strong, nonatomic) IBOutlet UITableView *tblWithSections;

5.Make the changes in .m file

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    NSDateFormatter* format = [[NSDateFormatter alloc]init];
    NSString* strDate = @"20/06/2013";
    [format setDateFormat:@"dd.MM.yyyy"];
    NSLog(@"%@", [format dateFromString:strDate]);
    
    
    arrHeader = [[NSMutableArray alloc]initWithObjects:@"Physics",@"Math", nil];
    arrPhysics = [[NSMutableArray alloc]initWithObjects:@"Amit",@"Sudheer", nil];
    arrMathematics = [[NSMutableArray alloc]initWithObjects:@"Kiran",@"Vijay", nil];
    
// Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [arrHeader count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0)
    {
        return [arrPhysics count];
    }
    else if(section==1)
    {
        return [arrMathematics count];
    }
    else
        return 0;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [arrHeader objectAtIndex:section];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdent;
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    if(indexPath.section==0)
    {
        cell.textLabel.text = [arrPhysics objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [arrMathematics objectAtIndex:indexPath.row];
    }
    return cell;
}

6. Make sure the neccessary connection like UITableViewDelegate and UITableview Datasource.

Tuesday, 18 June 2013

Date formatter in iOS (NSDateFormatter) OR Reset the date formatter


-(void)currentDateWithRequiredFormatter
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    
    // Current Date with default formatter..
    
    NSLog(@"Current Date with default Formatter = %@",[NSString stringWithFormat:@"%@",[NSDate date]]);
    
    // Set Date format as per your requirment..
    
    [formatter setDateFormat:@"dd/MM/yyyy"];
    
    // Set Current date to which you want...
    
    [formatter stringFromDate:[NSDate date]];
    
    NSLog(@"Current Date after using date Formatter = %@",[formatter stringFromDate:[NSDate date]]);
}

Out Put :

Current Date with default Formatter = 2013-06-19 04:43:35 +0000

Current Date after using date Formatter = 19/06/2013




// Second scenario
    
    // Suppose you have date as a string like..
     NSString* strGettingDate = @"24/05/2013";//dd/MM/yyyy
    
    NSLog(@"Without formatter : %@",strGettingDate);
    
    // And you want to 05.24.2013 // MM.dd.yyyy
    
    [formatter setDateFormat:@"MM.dd.yyyy"];
    [formatter dateFromString:strGettingDate];
    NSLog(@"Using date Formatter = %@",[formatter dateFromString:strGettingDate]);







Out Put :

Without formatter : 24/05/2013
Using after using date Formatter = (null)





 NSDateFormatter* formatter = [[NSDateFormatter alloc]init];

    // So solve this issue you have to set date format in which format you are getting first like..
    
    // Suppose you have date as a string like..
    NSString* strGettingDate = @"24/05/2013";//dd/MM/yyyy
    
     NSLog(@"Without formatter : %@",strGettingDate);
    
    [formatter setDateFormat:@"dd/MM/yyyy"];
    NSDate* dtDate = [formatter dateFromString:strGettingDate];
    
    // Now change the date format in which you want like..
    [formatter setDateFormat:@"MM.dd.yyyy"];
     NSString* strRequiredDate = [formatter stringFromDate:dtDate];
    
     NSLog(@"Using date Formatter : %@",strRequiredDate);

OUT PUT : 

 Without formatter : 24/05/2013
 Using date Formatter : 05.24.2013



You can also go for :
http://www.youtube.com/watch?v=p1FC9T7YdKI


Friday, 7 June 2013

UIAlertView in iOS,Iphone


Declare  UIAlertView in .h file like following...

UIAlertView * alert;

-(void)alertProgress
{
    alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    UIActivityIndicatorView* activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [alert addSubview:activity];
    activity.center = CGPointMake(140,80);
    activity.color = [UIColor whiteColor];
    [activity startAnimating];
    [alert show];
}
-(void)stopAlertProgress
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

Monday, 20 May 2013

Get The Max Value from an NSArray



-(void)findMax
{
    NSArray *numberArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:10], [NSNumber numberWithInt:20], [NSNumber numberWithInt:1000], nil];
    NSInteger highestNumber;
    NSInteger numberIndex;
    for (NSNumber *theNumber in numberArray)
    {
        if ([theNumber integerValue] > highestNumber) {
            highestNumber = [theNumber integerValue];
            numberIndex = [numberArray indexOfObject:theNumber];
        }
    }
    NSLog(@"Highest number: %i at index: %i", highestNumber, numberIndex);
}


Out Put :

Highest number: 1000 at index: 2


Using KVC


[self setValue:[NSNumber numberWithInt:10] forKey:@"number1"];
[self setValue:[NSNumber numberWithInt:20] forKey:@"number2"];
[self setValue:[NSNumber numberWithInt:1000] forKey:@"number3"];
   
NSArray *numberArray = [NSArray arrayWithObjects:[self valueForKey:@"number1"], [self valueForKey:@"number2"], [self valueForKey:@"number3"], nil];
    
    
    int max = [[numberArray valueForKeyPath:@"@max.intValue"] intValue];
    
   NSLog(@"Highest number: %i",max);

Out Put :




Highest number: 1000

Thursday, 16 May 2013

Using NSMutableString print the Triangle


-(void)triAngle
{
    NSMutableString *strTriangle = [[NSMutableString alloc]init];
    for (int i = 0; i<3; i++)
    {
        for (int j = 0; j<=i; j++)
        {
            if(j==0 && i==0)
            {
                [strTriangle setString:@"\n*"];
            }
            else
             [strTriangle appendString: @"*"];
            
        }
      [strTriangle appendString: @"\n"];
    }
    NSLog(@"%@",strTriangle);
    
}

OutPut :

*
**
***

Steps to show the hidden files on your mac

1. Launch the Terminal(/Application/Utilities)

OR

Hold the command key and press the space bar you could see the search box right corner of your mac and type terminal.

Click on spotlight search icon  right corner of your mac and type terminal.

2. Type the following command exactly

defaults write com.apple.Finder AppleShowAllFiles TRUE

then for relaunch the finder Type the following command exactly..

killall Finder 

Wednesday, 15 May 2013

What is KVC and KVO



------------------KVC--------------
Character.h

#import <Foundation/Foundation.h>

@interface Character : NSObject
{
    NSString *Name;
    NSInteger Rating;
}
@property (nonatomic, copy) NSString *Name;
@property(nonatomic, assign) NSInteger Rating;

@end



Character.m
#import "Character.h"

@implementation Character
@synthesize Name;
@synthesize Rating;

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"required"])
    {
        //NSLog(@"%@",[object valueForKeyPath:keyPath]);
         NSLog(@"%@",[change  objectForKey:NSKeyValueChangeOldKey]);
    }
}

@end




-(void)KVC
{

    Character *conharector1;    
    // KVC...
    conharector1 = [[Character alloc] init];
    [conharector1 setValue:@"Amit" forKey:@"Name"];
    [conharector1 setValue:[NSNumber numberWithInt:10] forKey:@"Rating"];
    
    //Done! 
    
    NSString *GetconharectorName = [conharector1 valueForKey:@"Name"];
    NSNumber *GetconharectorRating = [conharector1 valueForKey:@"Rating"];
    
    NSLog(@"%@ Rating = %d  ", GetconharectorName, [GetconharectorRating intValue]);
    
}


OUTPUT :


2013-05-16 10:58:20.263 StoryBoardApplication[536:c07] Amit Rating = 10  


----------------------- KVO---------------

Employee.h
#import <Foundation/Foundation.h>
@interface Employee : NSObject
{
    int required;
}
@property(nonatomic,assign) int required;

-(id)init;
@end







Employee.m

#import "Employee.h"

@implementation Employee
@synthesize required;
-(id)init
{
     self = [super init];
    if(self)
    {
        required = 1;
    }
    return self;
}

@end



Manager.h

import <Foundation/Foundation.h>

@interface Manager : NSObject

@end

Manager.m


#import "Manager.h"

@implementation Manager
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"required"])
    {
        //NSLog(@"%@",[object valueForKeyPath:keyPath]);
        NSLog(@"%@",[change  objectForKey:NSKeyValueChangeNewKey]);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Changed" message:@"Required value has changed By Employee" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}
@end



Following method you can call whereever you want to :

-(void)KVO
{
    Employee *employee = [[Employee  alloc]init];
    Manager *manager = [[Manager alloc]init];
    
    [employee addObserver:manager forKeyPath:@"required" options:NSKeyValueObservingOptionNew   context:NULL];
    [employee setRequired:67];
    [employee removeObserver:manager forKeyPath:@"required"];
    

OUTPUT :

2013-05-16 10:56:25.601 StoryBoardApplication[512:c07] 67

and an alert like : Required value has changed By Employee

I hope it will be help you....





Tuesday, 14 May 2013

Code For NSPredicate in objective c(IOS)


    Add a new file(NSObject) in your working application and add following code first..

.h

-(id)initWithName:(NSString *) name;
-(NSString *)description;


.m

@synthesize strName;

-(id)initWithName:(NSString *) name
{
    self = [super init];
    if(self)
    {
        self.strName = [name copy];
        
        
    }
    return self;
}

-(NSString *)description
{
    return self.strName;
}

After that import this file in your desired view and add Following piece of code..




    Employee *emp = [[Employee alloc]initWithName:@"Amit"];
    Employee *emp1 = [[Employee alloc]initWithName:@"Sudheer"];
    Employee *emp3 = [[Employee alloc]initWithName:@"Kiran"];
    
    NSMutableArray *arr1  = [[NSMutableArray alloc]initWithObjects:emp,emp1,emp3 ,nil];
    
    
    NSPredicate *nsPred1 = [NSPredicate predicateWithFormat:@"strName == 'Kiran'"];
    NSPredicate *nsPred2 = [NSPredicate predicateWithFormat:@"strName.length <5"];
    NSPredicate *nsPred3 = [NSPredicate predicateWithFormat:@"strName BEGINSWITH[c] 'a'"];
    NSPredicate *nsPred4 = [NSPredicate predicateWithFormat:@"strName ENDSWITH[c] 'r'"];
    
    NSArray *arrFilterdArray1 = [arr1 filteredArrayUsingPredicate:nsPred1];
    NSArray *arrFilterdArray2 = [arr1 filteredArrayUsingPredicate:nsPred2];
    NSArray *arrFilterdArray3 = [arr1 filteredArrayUsingPredicate:nsPred3];
    NSArray *arrFilterdArray4 = [arr1 filteredArrayUsingPredicate:nsPred4];
    NSLog(@"%@",arrFilterdArray1);
    NSLog(@"%@",arrFilterdArray2);
    NSLog(@"%@",arrFilterdArray3);
    NSLog(@"%@",arrFilterdArray4);


Out Put


2013-05-15 11:50:26.918 StoryBoardApplication[390:207] (
    Kiran
)
2013-05-15 11:50:28.037 StoryBoardApplication[390:207] (
    Amit
)
2013-05-15 11:50:28.661 StoryBoardApplication[390:207] (
    Amit
)
2013-05-15 11:50:29.276 StoryBoardApplication[390:207] (
    Sudheer
)

Tuesday, 7 May 2013

What's the difference between frame and bounds in IOS



The frame  shows  the origin and size of a view in superview coordinates.
The bounds Shows the origin in the view’s coordinates and its size (the view’s content may be larger than the bounds size).




 NSLog(@"%@",NSStringFromCGRect(self.btnNext.frame));
 NSLog(@"%@",NSStringFromCGRect(self.btnNext.bounds));

OutPut :

frame :
2013-05-07 15:48:48.998 FirstStoryBoardViewController[1825:c07] {{51, 153}, {181, 44}}
Internal error [IRForTarget]: Couldn't rewrite external variable _ZZ52-[KMGViewController($__lldb_category) $__lldb_expr:]E19$__lldb_expr_result

Bounds :
2013-05-07 15:49:00.941 FirstStoryBoardViewController[1825:c07] {{0, 0}, {181, 44}}
Internal error [IRForTarget]: Couldn't rewrite external variable _ZZ52-[KMGViewController($__lldb_category) $__lldb_expr:]E19$__lldb_expr_result

Wednesday, 1 May 2013

Difference between NSArray and NSMutableArray in Objective C


      NSArray is Immutable and ordered collection i.e you can not update if once its  initialized while
    NSMutableArray  is Mutable and ordered collection  i.e you can add more object as per your requirement     



Example:

    NSArray *arr1 = [[NSArray alloc]initWithObjects:@"Amit",@"Jai",@"Sudheer"nil];
        NSLog(@"%@",arr1);
outPut :

2013-05-02 11:34:43.372 FirstStoryBoardViewController[418:c07] (
    Amit,
    Jai,
    Sudheer
)

[arr1 addObject:@"Kiran"]; if we try to do like this,we get build error as 


    No visible @interface for 'NSArray' declares the selector 'addObject':

   NSMutableArray *Muarr1 = [[NSMutableArray alloc]initWithObjects:@"Amit",@"Jai",@"Sudheer"nil];
  NSLog(@"%@",Muarr1);

outPut :

2013-05-02 11:34:46.226 FirstStoryBoardViewController[418:c07] (
    Amit,
    Jai,
    Sudheer
)



 [Muarr1 addObject:@"Kiran"];
NSLog(@"%@",Muarr1);
 outPut :

  2013-05-02 11:37:37.178 FirstStoryBoardViewController[458:c07] (
    Amit,
    Jai,
    Sudheer,
    Kiran
)


      

Differences Between NSArray And NSSet in Objective C



NSSet
  • Primarily access items by comparison
  • Unordered
  • Does not allow duplicates
NSArray
  • Can access items by index
  • Ordered
  • Allows duplicates
Example


    NSSet *set = [NSSet setWithObjects:@"Amit",@"Rakesh",@"Kiran",@"Jai",@"zebra",@"Amit",@"Kiran"nil];
    NSArray *arr = [[NSArray alloc]initWithObjects:@"Amit",@"Rakesh",@"Kiran",@"Jai",@"zebra",@"Amit",@"Kiran"nil];
    
    NSLog(@"%@",set);
    NSLog(@"%@",arr);


outPut

2013-05-01 11:45:53.881 FirstStoryBoardViewController[630:c07] {(
    Jai,
    zebra,
    Rakesh,
    Amit,
    Kiran
)}
2013-05-01 11:45:54.384 FirstStoryBoardViewController[630:c07] (
    Amit,
    Rakesh,
    Kiran,
    Jai,
    zebra,
    Amit,
    Kiran
)