Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-27-2009, 11:05 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 6
Default Getting the selectedIndex of the currently selected tab from a viewcontroller

I'm developing an iPhone application with a tabbar and multiple viewcontrollers. I'm a novice concerning the iPhone and the SDK, so bear with me.

I'd like to get the selectedIndex of the tabbar from my viewcontroller. But no matter what I try to do, it returns (null) and the application crashes as soon as I switch to another tab section.

I tried calling the following in my FirstViewController.m:
Code:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  NSLog(@"%@", appDelegate.tabBarController.selectedIndex);
and:
Code:
NSLog(@"%@", self.tabBarController.selectedIndex);
But all return (null).

How would I do this correctly? The code for my viewcontroller and appdelegate follows. All the views are designed in Interface Builder.

MyAppDelegate.h
Code:
#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  UIWindow *window;
  UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end
MyAppDelegate.m
Code:
#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize window, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  [window addSubview:tabBarController.view];
}

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end
FirstViewController.h
Code:
#import <UIKit/UIKit.h>

<UITableViewDataSource, UITableViewDelegate> {
@interface FirstViewController : UITableViewController {
}

@end
FirstViewController.m
Code:
#import "FirstViewController.h"

@implementation FirstViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.title=@"Agenda";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc { 
	[super dealloc];
}
mensch is offline   Reply With Quote
Old 10-27-2009, 11:52 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

%d, not %@
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 10-27-2009, 12:00 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 6
Default

Ouch, that's obvious. I could have sworn I already used %d instead of %@ while testing.

Calling it like in viewDidLoad in FirstViewController.m returns the correct index:
Code:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  NSLog(@"%d", appDelegate.tabBarController.selectedIndex);
The only problem I now have is that it returns the value only once. My guess is that that's because the view has already been loaded before. Is there a way to work around this?
mensch is offline   Reply With Quote
Old 10-27-2009, 12:18 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

viewWillAppear
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 10-28-2009, 06:10 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 6
Default

Great! Thanks, that did the trick.
mensch is offline   Reply With Quote
Reply

Bookmarks

Tags
uitabbarcontroller, uittabbar, viewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 273
20 members and 253 guests
ADY, Bertrand21, Dani77, HemiMG, iDifferent, IphoneSdk, jakerocheleau, JasonR, jimbo, macquitzon216, MACralik, mer10, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84, Sunny46
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:01 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0