Wednesday 18 July 2012

Array in iOS

Array Using iPhone Project:

.h File
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSMutableArray *Name;
    NSMutableArray *Phone;
    NSMutableArray *Email;
    
    int i;
}
-(void)arraylist;

@property(nonatomic,retain)IBOutlet UILabel *label1;
@property(nonatomic,retain)IBOutlet UILabel *label2;
@property(nonatomic,retain)IBOutlet UILabel *label3;

-(IBAction)next:(id)sender;

@end


.m File
@synthesize label1,label2,label3;


- (void)viewDidLoad
{
    [super viewDidLoad];
    i=0;
    
    Name=[[NSMutableArray alloc]init];
    Phone=[[NSMutableArray alloc]init];
    Email=[[NSMutableArray alloc]init];
   
    NSString *content=@"Name/n5667890/nName@email.com/brName1:/n5667990/nName1@email.com/brName2/n345235476/nName2@email.com/brName3/n45678/nName3@email.com";
    NSArray *components = [content componentsSeparatedByString:@"/br"];
   
    for(int k=0;k<[components count];k++)
    {
        
        NSString *hai = [components objectAtIndex:k];
        NSArray *components1 = [hai componentsSeparatedByString:@"/n"];
        NSString *Name1=[components1 objectAtIndex:0];
         NSLog(Name1);
        NSString *Phone1=[components1 objectAtIndex:1];
        NSLog(Phone1);
        NSString *Email1=[components1 objectAtIndex:2];
        NSLog(Email1);
        
        [Name addObject:Name1];
        [Phone addObject:Phone1];
        [Email addObject:Email1];
    }
    [self arraylist];
}

-(void)arraylist
{
    if(i<3)
    {
        label1.text=[Name objectAtIndex:i];
        label2.text=[Phone objectAtIndex:i];
        label3.text=[Email objectAtIndex:i];
        
    }
    else
    {
        UIAlertView *objAlert = [[UIAlertView alloc] initWithTitle:@"Your Score" message:@"5" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [objAlert setTag:1];
        [objAlert show];
        [objAlert release];
    }
}

-(IBAction)next:(id)sender
{
    i++;
    [self arraylist];
}

.Xib File:
Design the page



Connecting the Outlets and Run





No comments: