views:

219

answers:

1

Hello All.

This is my app im working on. http://twitpic.com/yrzpo and it has a modal view that i want to let the user app things to the routines list. http://twitpic.com/yrzs3

The table view on the first page, has a data source of a NSMutableArray. On the second page, i would like to add to that array, by typing in the top text field, so when the modal view pops down, what was typed in the the field was added to the list.

Surely there is a way to do this.

Keep in mind, that my template for this app was a tab bar application. FirstViewController.h

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

NSMutableArray *routines;
}

@property (nonatomic, retain) NSMutableArray *routines;

- (IBAction)showNewEventViewController;   



@end

FirstViewController.m

#import "FirstViewController.h"
#import "NewEventViewController.h"

@implementation FirstViewController

@synthesize routines;



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [routines count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...
NSString *cellValue = [routines objectAtIndex:indexPath.row];
[cell.textLabel setText:cellValue];

return cell;
}


- (IBAction)showNewEventViewController {    

NewEventViewController *controller = [[NewEventViewController alloc] initWithNibName:@"NewEventView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];

[controller release];
}




- (void)viewDidLoad {

routines = [[NSMutableArray alloc] init];

[routines addObject:@"Hello"];
[routines addObject:@"Temp"];
[routines addObject:@"Temp2"];
[routines addObject:@"Temp3"];
[routines addObject:@"Temp4"];


}




- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[routines release];

[super dealloc];
}

@end

NewEventViewController.h

#import <UIKit/UIKit.h>


@interface NewEventViewController : UIViewController {

IBOutlet UITextField *RoutineTitle;

IBOutlet UITextField *RoutineInvolvment;

}
-(IBAction)done;


@end

NewEventViewController.m

#import "NewEventViewController.h"
#import "FirstViewController.h"




@implementation NewEventViewController



-(IBAction)done{


[RoutineTitle resignFirstResponder];
[RoutineInvolvment resignFirstResponder];

NSString *myString = RoutineTitle.text;
FirstViewController *FirstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
NSMutableArray *routines;


NSLog(@"Log the String: %@", myString);

FirstView.routines = routines;

[routines addObject:myString];



NSLog(@"Log Array :%@", FirstView.routines);




[self dismissModalViewControllerAnimated:YES];


}


- (void)viewDidLoad {
[super viewDidLoad];

}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {

[super dealloc];
}


@end

I urge anyone who can, please have a look at the code, and tell me what I am doing wrong. Im new to this game, ( and esp to apps that are not a single view)

I hope I have made this clear.

Thanks a lot in advance.

Sam

PS. my last few questions are all culminated in this one, for everyones ease.

Thanks.

+1  A: 

Started to look through your code and ran out of time. There are a number of issues.

The idea is to pass a pointer to the routines array to the NewEventViewController, have the NewEventViewController add to it, then on "Done" dismiss the NewEventViewController and reload the UITableView with the data in the now modified routines array.

In NewEventViewController.h you need to define NSMutableArray to point to the routines array you have in FirstViewController.h

 @interface NewEventViewController : UIViewController {

     IBOutlet UITextField *RoutineTitle;
     IBOutlet UITextField *RoutineInvolvment;
     NSMutableArray *routines;

    }

    @property(nonatomic, retain)  NSMutableArray *routines;
    -(IBAction)done;

    @end

In NewEventViewController.m you need to add the following:

@implementation NewEventViewController

@synthesize routines;

-(IBAction)done{

    // ...you can get the string directly
 [routines addObject:RoutineTitle.text]; 
 [self dismissModalViewControllerAnimated:YES];


}


- (void)dealloc {

 [super dealloc];
 [routines release];

}

Add to FirstViewController the following:

IBOutlet UITableView *myTableView;

and

@property (nonatomic, retain) NSMutableArray *routines;
@property (nonatomic, retain) UITableView *myTableView;

and in FirstViewController, add the following:

  @synthesize routines, myTableView;

    - (void)viewWillAppear:(BOOL)animated
        {
         [self.myTableView reloadData];

        }

   - (IBAction)showNewEventViewController {    



NewEventViewController *controller = [[NewEventViewController alloc] initWithNibName:@"NewEventViewController" bundle:nil];
 controller.routines=routines;
 controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
 [self presentModalViewController:controller animated:YES];

 [controller release];
}

        - (void)dealloc {
        [routines release];
        [myTableView release];
        [super dealloc];
        }

Make sure you delete all of this stuff...Not required. You already have a pointer to the NSMutableArray *routines that you're passing when you pushViewController.

NSString *myString = RoutineTitle.text;
FirstViewController *FirstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
NSMutableArray *routines;


NSLog(@"Log the String: %@", myString);

FirstView.routines = routines;
Jordan
The other thing I wanted to add. If you want to keep track of multiple items (title and involvement) then you'll need either a data model (class) or a NSMutableDictionary to keep track of each item in your UITableView. The example above using a single-item array. You'll probably want more than that to keep track of the details for each workout, or whatever.
Jordan
OK, so all is working now, correctly, EXCEPT the table view does not reload. (with the new data). Some logging shows that new items show up in both the routine arrays, but not on the table.
Sam Jarman
Two things to check: 1) Is your IBOutlet tableView connected properly in IB? 2) Is [self.myTableView reloadData] being called from - (void)viewWillAppear:(BOOL)animated?
Jordan