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];
}