1

i need your help. Basically I created a small scrollView and a pageControl inside my main view controller. Now when ever a button inside a scroll view is pressed I lose the value of every property in my mainViewController. To help you get a clearer picture let me explain:

(NoteViewController.m) This is the action the button that is pressed from the scrollview responds to

- (IBAction)removePerson:(UIButton *)sender {
MainViewController *remover = [[MainViewController alloc] init];
[remover removePersonWithPage:pageNumber];
[self.view removeFromSuperview];
[remover release]; }

(MainViewController.m)

- (void)removePersonWithPage:(int)page {
// The managedObjectContext is lost the moment it leaves MainViewController.m and goes to NoteViewController.m
// so you need to reload the managedObjectContext
if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(OrdersAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
}

// Get the list of people (Persons) from the managed Object Context
arrayOfPeople = [[NSMutableArray alloc] initWithArray:[self fetchDataWithEntity:@"Person" andSortKey:@"pageId"]];

// Find a specific person to delete using their page number and delete it finally remove it from array
NSManagedObject *personToDelete = [arrayOfPeople objectAtIndex:page];
[managedObjectContext deleteObject:personToDelete];
[arrayOfPeople removeObjectAtIndex:page];

// kNumberOfPages is replaced with the new number of people
kNumberOfPages = arrayOfPeople.count;

/* This is where problem occurs */
self.pageControl.numberOfPages = kNumberOfPages;
NSLog(@"The number of pages in the page control in remove is: %d", self.pageControl.numberOfPages); 

[self saveObjectContext]; 
}

So everything works but when I get to the NSLog at the end there, it returns 0 when it ought to be returning the number of pages in the database. I've been working on this for days now and can't figure it out, please help. Thanks

NSCoder
  • 1,594
  • 5
  • 25
  • 47
  • If you place NSLogs throughout this method call, when does the page count go wrong? Is it right on entering? Is this where kNumberOfPages is first initialized? – Kinetic Stack Dec 27 '11 at 18:02
  • Where do you get your pageControl property from? Is it wired in a nib? If so, you are manually allocating that remover view controller, so there will be no wired control there. – Jason Coco Dec 27 '11 at 19:20
  • Wow this is really cool, I didn't think I would get an answer so quick. Thanks guys. So to answer KineticStack's question, yes it is right on entering, right after the call ([remover removePersonWithPage:pageNumber]) from the NoteViewController.m. I lose control of all my properties and even my managed object context, as you can see, I had to reload. And as for Jason Coco's, yes, I've wired it up using interface builder created the IBOutlets, Properties and Synthesized. It even works on all other methods (actions) but this one. Is there a way to share between these two view controllers? Thanks – NSCoder Dec 27 '11 at 20:14
  • Don't you think that could be because you're creating a new `MainViewController` every time the delete button is tapped? We need a higher level overview of your application. Where does `NoteViewController` sit relative to `MainViewController`? – Mark Adams Dec 27 '11 at 20:34
  • Here is what the app looks like. Its basically a layout. ![Design](http://farm8.staticflickr.com/7023/6583623393_9db672c66e_z.jpg). So if you see the picture you have a MainViewController which fills the whole screen, a scrollView inside it, and the NoteViewControllers are subviews of the scrollView, with each subview having a remove button. – NSCoder Dec 27 '11 at 20:59
  • Okay guys, I may have figured out a way to do it. I was googling and I found this [link](http://stackoverflow.com/questions/1123920/how-do-i-shared-an-object-between-uiviewcontrollers-on-iphone). They used a singleton object to share data between the different views. Is this possible with an IBOutlet object, i.e. can i use it to share a page control over multiple view controller? – NSCoder Dec 29 '11 at 11:37

0 Answers0