Wednesday, November 21, 2012

Create a Global access for variable and array instead of AppDelegate declaration:

  1. Create a NSObject class


#import <Foundation/Foundation.h>

@class Globalclass; //create a global class name

extern Globalclass *Global; // create a instance for global class

@interface Globalclass : NSObject
NSMutableArray *Pointarray1; // global array declaration

@property(nonatomic,readwrite) NSMutableArray *Pointarray1;

+(void)globalaccess; // create a class method



#import "Globalclass.h"
Globalclass *Global; // create a instance for global class

@implementation Globalclass

@synthesize Pointarray1;


Global=[[Globalclass alloc]init]; // allocate global class



#import "ViewController.h"
#import "Tableview.h"
#import "Globalclass.h" // import Global class

@interface ViewController ()


@implementation ViewController
@synthesize btn;

- (void)viewDidLoad
[Globalclass globalaccess]; // To call global class method
Global.Pointarray1=[[NSMutableArray alloc]init]; // initate the created global array
[super viewDidLoad];



-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch ;
touch = [[event allTouches] anyObject];
if ([touch view] == self.view){
CGPoint location = [touch locationInView:touch.view];
UIImageView *img1=[[UIImageView alloc]init];
[img1 setImage:[UIImage imageNamed:@"bluedot.gif"]];
img1.frame=CGRectMake(2, 2,location.x,location.y);
[self.view addSubview:img1];
NSLog(@"point........ %f .. %f",location.x,location.y);
[Global.Pointarray1 addObject:[NSString stringWithFormat:@"X=%f,Y=%f",location.x,location.y]];
// To access the created array
NSLog(@"point array==>%@",Global.Pointarray1);


#import "Tableview.h"
#import "Globalclass.h" // import Global class

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of rows in the section.
return [Global.Pointarray1 count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// Configure the cell...
NSString *cellValue = [Global.Pointarray1 objectAtIndex:indexPath.row]; // display the array value in table view.

cell.textLabel.text = cellValue;
return cell;

No comments:

Post a Comment