开发者

Editing a NSMutable Array, defined in one class, from a Modal View Controller

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"




@imp开发者_StackOverflow中文版lementation 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

Please have a look at the code and tell me what I am doing wrong. I'm new to this game (and especially to apps that are not a single view).


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜