开发者

iPhone Reading Property From Other Class In UITab

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)goTo开发者_开发百科Chapter:(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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜