Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Extreme newbie NSMutableArray with IBAction question

PeteXPeteX Posts: 7New Users
Hi,
To introduce myself I must appologize and say that I am an extreme newbie. I have been trying to learn Objective C, Cocoa, iOS, Mac Os programming on and off for a long time now, but I can never seem to get past the basics.
So I decided to just jump in and try and build something, and now I am stuck at something basic, and I cannot find any helpfull info (probably because I am using the wrong terminology in searching).

I am trying to build a simple app that has a button. When a user taps the button I record the number of times the user has tapped the button and the exact time when it has been tapped.
This is working perfect.

But now I am trying to store the above collected info into a mutable array, and here is where it gets confusing for me.

Obviously I cannot alloc and init my mutable array inside my button IBAction, as it would just recreate the Array everytime the IBAction gets triggered.

So a good place to alloc and init my array seemed inside the didFinishLaunchingWithOptions method in my AppDelegate file.

But if I do this, my array is undeclared in my ViewController file.
So how do I make my initialized array available to the ViewController ?

I have tried to declare the array in my viewcontroller header file, which is imported in my appdelegate implementation file, but xcode still does not see the array.

Is this what delegation is about (a concept I am having a very hard time with), or do I need to read up on something different ?

My code below is a complete mess now, as I have been trying and moving the parts of the array around in the two separate files.
I can perfectly create an array inside a single method, but how do I populate an array from a button click ? Or is this not recommended at all and am I following the wrong path ?

AppDelegate.m

#import \"AppDelegate.h\"
#import \"ViewController.h\"

@implementation AppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];


NSMutableArray *tapsArray = [NSMutableArray arrayWithCapacity: 2];
tapsArray = [[NSMutableArray alloc] init];
NSLog(@\"Inside the didFinishLaunchingWithOptions method\");

return YES;
}

- (void)dealloc {
[tapsArray release];
[viewController release];
[window release];
[super dealloc];
}


@end


ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

IBOutlet UILabel *numberOfTaps;
IBOutlet UILabel *timeOfTaps;
NSString *thedate;
double countTaps;
//NSMutableArray *tapsArray;

}

@property (nonatomic, retain) UILabel *numberOfTaps;
@property (nonatomic, retain) UILabel *timeOfTaps;
//@property (nonatomic, retain) NSMutableArray *tapsArray;

- (IBAction) countNumberOfTaps;

@end


ViewController.m

#import \"ViewController.h\"

@implementation ViewController

@synthesize numberOfTaps,timeOfTaps;

- (IBAction) countNumberOfTaps
{
//int numberOfObjects = [tapsArray count];
countTaps = countTaps + 1;
[numberOfTaps setText:[NSString stringWithFormat:@\"%g\", countTaps]];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@\"HH:mm:ss:SS\"];

thedate = [dateFormatter stringFromDate:[NSDate date]];

[timeOfTaps setText:thedate];

[tapsArray addObject:thedate];
NSLog(@\"The array contains %i objects.\", [tapsArray count]);


}



Any help would be extremely appreciated.

Regards,
Peter
Post edited by PeteX on

Replies

Sign In or Register to comment.