views:

15

answers:

1

I'm new to Objective-C and have a little bit of problem. I made an app that has a button that switches to tab number 2 in the app but I want it to get specific data from the database so I've written code in which I pass from the first view a number which is the id of the row I need to the second view

    @synthesize goTo;
-(IBAction)goToChapter:(id)sender{      
    self.goTo= (int)10 ;
    self.tabBarController.selectedViewController 
    = [self.tabBarController.viewControllers objectAtIndex:1];
}

So in the second class i tried this:

- (void)viewDidLoad {
    [super viewDidLoad];
    FirstViewController *fvc=[[FirstViewController alloc] init];    
    int inx =  (int)fvc.goTo ;
    [self getPageContent:inx];
}

but what I got is that the goTo is with a zero not 10 as suppose to be. What am I doing wrong and how can this be handled?

Thanks in advance :)

Emad Hegab

+1  A: 

Try this:

- (void)viewDidLoad {
    [super viewDidLoad];
    FirstViewController *fvc = (FirstViewController *)[self.tabBarController.viewControllers objectAtIndex:0];
    int inx = fvc.goTo;
    [self getPageContent:inx];
}

This assumes your FirstViewController is at index 0.

You were creating a new instance of FirstViewController instead of getting a reference to the already existing instance.

You also don't need to do the (int) casts.

Also, the viewDidLoad will probably only run the first time you go to the second tab. So when the user tabs back to the first VC and picks another chapter, the viewDidLoad will not execute. You might want to use viewDidAppear instead.

aBitObvious
thanks man it work like a magic trick :) many many thanks
Mohamed Emad Hegab