Wondering if this programming method is generally good to use
Ok well after my first iPhone application i really started to grasp the concept of the entire UIKit but one thing i still dont fully comprehend is using data from one view controller to the other. So i came up with my own little solution which is working fine for me right now but im wondering what your opinion would be upon my programming methods. Since i am parsing information and using information in other view controllers i decided to start using structs for a lot of the main data for each view controller. This to me makes it much more simple to pass around data from view controller to view controller, i even started placing my tableviews in the structs and assigning them to the table view in the nib file so i can be in another view controller and when needed i can manipulate the table view from the other view controller. So knowing this what do you think about this method?
I guess I would need to see an example of what you are doing, because something sounds a little off about your explanation. Perhaps incorrect terminology.
Well for me its working so idk i might stick with it unless one of you can relay to me why this would either break my program or get it sent back to me from apple.
But heres the portion of code and each view controller is exactly the same as the one im posting.
Code:
struct MyMenu {
NSMutableArray *title;
NSMutableArray *inventory;
NSMutableArray *objectID;
NSMutableArray *image;
UITableView *tableView;
bool userWantsMoreObjects;
int thumbnailId;
int overallCount;
int pageCount;
int count;
}myStruct;
@interface MyViewController : UITableViewController {
}
//ans heres the .m file
- (void)viewDidLoad {
[super viewDidLoad];
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible=FALSE;
self.title=@"Objects";
myStruct.tableView = self.tableView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
return YES;
}
#pragma mark -
#pragma mark My Methods
//grabs more information from the server and adds it to the browse.tableView
-(void)grabMoreStructData{
if(myStruct.overallCount==browse.count){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry"
message:@"There are no more objects left to get from server"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
else{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = TRUE;
Struct.userWantsMoreObjects=TRUE;
StructMenuData *bmd = [[StructMenuData alloc]init];
[bmd parseMoreData];
[bmd release];
}
}
-(void)checkOverallCount{
// if(Struct.overallCount==Struct.count)
// self.navigationItem.rightBarButtonItem=nil;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
return Struct.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSInteger row = [indexPath row];
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
UILabel *label = [[UILabel alloc]init];
label.tag = objectTitleTag;
label.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:label];
[label release];
}
if(row==browse.count-1&&browse.count!=browse.overallCount){
[cell.inventoryNumberLabel setFont:[UIFont boldSystemFontOfSize:15]];
[cell.inventoryNumberLabel setFrame:CGRectMake(75, 20, 200, 20)];
[cell.inventoryNumberLabel setText:@"Load More Objects..."];
[cell.inventoryNumberLabel setTextColor:[UIColor blueColor]];
[cell.contentView addSubview:cell.inventoryNumberLabel];
[cell.artTitleLabel setText:[NSString stringWithFormat:@"You have %i Objects left",browse.overallCount-browse.count]];
[cell.artTitleLabel setFrame:CGRectMake(75, 35, 200, 20)];
cell.artTitleLabel.font = [UIFont boldSystemFontOfSize:10];
cell.artTitleLabel.textColor = [UIColor grayColor];
[cell.contentView addSubview:cell.artTitleLabel];
cell.imageView.image=nil;
return cell;
[cell release];
}
else{
[cell.inventoryNumberLabel setText:[browse.inventoryNumber objectAtIndex:row]];
[cell.inventoryNumberLabel setTextColor:[UIColor blackColor]];
[cell.inventoryNumberLabel setFont:[UIFont systemFontOfSize:18]];
[cell.inventoryNumberLabel setFrame:CGRectMake(100, 5, 200, 20)];
[cell.contentView addSubview:cell.inventoryNumberLabel];
[cell.artTitleLabel setText:[browse.artTitle objectAtIndex:row]];
[cell.artTitleLabel setFrame:CGRectMake(100, 25, 200, 20)];
[cell.artTitleLabel setTextColor:[UIColor blackColor]];
[cell.artTitleLabel setFont:[UIFont systemFontOfSize:15]];
[cell.contentView addSubview:cell.artTitleLabel];
UIImage *theImage= [[UIImage alloc]initWithData:[browse.mainImageData objectAtIndex:row]];
cell.imageView.contentMode = UIViewContentModeScaleToFill;
cell.imageView.image = theImage;
[theImage release];
return cell;
[cell release];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = TRUE;
if([indexPath row]==browse.count-1){
[self grabMoreBrowseData];
}
else {
Struct.artObjectFromBrowse=TRUE;
Struct.index=[indexPath row];
getArtObjectParser = [[GetArtObjectParser alloc]init];
[getArtObjectParser startParsing];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:TRUE];
}
#pragma mark -
#pragma mark UIAlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
self.navigationItem.rightBarButtonItem = nil;
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)dealloc {
getArtObjectParser=nil;
Struct.tableView = nil;
Struct.artTitle = nil;
Struct.artObjectId = nil;
Struct.mainImageData = nil;
Struct.inventoryNumber = nil;
[Struct.tableView release];
[Struct.artTitle release];
[Struct.artObjectId release];
[Struct.mainImageData release];
[Struct.inventoryNumber release];
[getArtObjectParser release];
[super dealloc];
}
Well i ment to say i manip[ulate the table view from a parser which i created as an entirely new object, and i havent had any memory issues so far. Can you show me a short program about how to pass data from one view to another. Or should i just take a ViewController and send it as a parameter in a funciton.
But this is wherei get lazy, but heres what i mean, when you start a NSURLConnection you eventually get to the delegate methods, so at the connectionDidFinishLoading method i want to reload my tableview as needed so the only way i can think about accessing my tableview from within its view controller is to pass it to the first function i call to start the parsing. but then i would have to pass it to the connectionDidRecieveResponse method and then the connectionDidRecieveData and so on. This to me does not sound correct.
this looks to me like you are experimentally getting closer to the Model-View-Controller design pattern/architecture. this is the direction to go. but to be honest, you should really go there faster. and by faster, i mean you should look up model view controller, get a good solid understanding of it, and use it from now on. it will separate your views from your data and make it so that you can register as many views as you'd like. its usefulness and the clarity it will bring to your code are priceless.
Thank you brian for the topics i should look up im going to look into that and thanks wahness for the advice and the support for a up and coming developer in need of assistance , ill look it up asap but do you know of any good sites explaining this topic other than the apple sdk site?