pListDemo In iPhone

This is the pListDemo example. In this example we will see how to use pList(PropertyList) in the code. So let see how it will be worked.

Step 1: Create a View base application using template. Give the application name “PlistDemo”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the PlistDemoViewController class for you. Expand Resources and notice the template generated a separate nib,PlistDemoyViewController.xib, for the “PlistDemo”.

Step 4: We need to add PropertyList in the Resource folder. So select Resources folder -> Add -> NewFile -> Select Resource -> PropertyList. Give the name of the PropertyList “data.plist”. See the below figure.

Step 5: In the PlistDemoViewController.h file, we have added four label in the file, so make the following changes in the file.

 

#import <uikit/UIKit.h>

#define kFileName @"data.plist"

@interface PlistDemoViewController : UIViewController {

IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
IBOutlet UILabel *label3;
IBOutlet UILabel *label4;

}

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

 

 

Step 6: Double click the PlistDemoViewController.xib file and open it to the view window. First drag label from the library and place it the view window. Give the label name “Line 1:”,”Line 2:”,”Line 3:”,”Line 4:”. Next drag four label from the library and place it to the view window(See the figure below). Connect File’s Owner icon to the View icon and select view. Drag File’s Owner icon to the first label and select label1, do it same for the next three label and select label2, label3 and label4. Now save the  PlistDemoViewController.xib file, close it, and go back to the Xcode.

Step 7: Open the PlistDemoViewController.m file and make the following changes in the file:

 

-(NSString *)dataFilePath{

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
return filePath;

}

- (void)viewDidLoad {
[super viewDidLoad];

NSString * filePath = [self dataFilePath];

if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{

NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
NSLog(@"file found : %i",[array count]);

label1.text = [array objectAtIndex:0];
label2.text = [array objectAtIndex:1];
label3.text = [array objectAtIndex:2];
label4.text = [array objectAtIndex:3];

[array release];
}
}

 

 

Step 8: Now compile and run the application in the Simulator.

You can Download SourceCode from here PlistShowDemoUsingUILable

Leave a Comment: