Luigi Saggese
Luigi Saggese

Reputation: 5379

UITabBarController as RootViewController in MonoTouch

I have a HomeScreen with a UITabBarController that i want to set as RootViewController, but when i do it i have following error

MonoTouch.Foundation.MonoTouchException has been thrown:

Objective-C exception thrown.  Name: NSUnknownKeyException Reason: [<UIApplication 0xc82d330> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tabBarController.

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSUnknownKeyException Reason: [<UIApplication 0xc82d330> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tabBarController.
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at Test.Application.Main (System.String[] args) [0x00000] in /Users/merqurio/Projects/Test/Test/Main.cs:17

I have also set Nib Name with same XIB name of File's Owner.

This is my project example. Click Here to Download Project

Upvotes: 0

Views: 784

Answers (2)

Paras Joshi
Paras Joshi

Reputation: 20541

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    _mainWindow = new UIWindow(UIScreen.MainScreen.Bounds);
    _mainTabBarController = new MainTabBarController();
    _mainWindow.AddSubview(_mainTabBarController.View);
    _mainWindow.MakeKeyAndVisible ();
    window.RootViewController = _mainTabBarController.View;
    return true;
}

and MainTabBarController like bellow..

public class MainTabBarController : UITabBarController
{
    public override void ViewDidLoad ()
    {
        ViewControllers = new UIViewController[]
        {
            new ViewControllerTab1(),
            new ViewControllerTab2(),
            new ViewControllerTab3(),
            new ViewControllerTab4(),
            new ViewControllerTab5()
        };

    }
}

for more info see this answer

Upvotes: 0

mani murugan
mani murugan

Reputation: 213

You have to set Viewcontrollers programmatically....like following

-(id)init {

   self = [super init];
    if (self) {


        HomePageViewController *homePageViewController = [[HomePageViewController alloc]initWithNibName:@"HomePageViewController" bundle:nil];
        WorkoutOfTheDayViewController *workoutOfTheDayViewController = [[WorkoutOfTheDayViewController alloc]initWithNibName:@"WorkoutOfTheDayViewController" bundle:nil];
        ScheduleViewController *scheduleViewController = [[ScheduleViewController alloc]initWithNibName:@"ScheduleViewController" bundle:nil];
        TrackingViewController *trackingViewController = [[TrackingViewController alloc]initWithNibName:@"TrackingViewController" bundle:nil];

        UINavigationController *navHome = [[UINavigationController alloc]initWithRootViewController:homePageViewController];
        UINavigationController *navWorkOut = [[UINavigationController alloc]initWithRootViewController:workoutOfTheDayViewController];
        UINavigationController *navSchedule = [[UINavigationController alloc]initWithRootViewController:scheduleViewController];
        UINavigationController *navTrack = [[UINavigationController alloc]initWithRootViewController:trackingViewController];

        homePageViewController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Home" image:[UIImage imageNamed:@"home.png"] tag:0];
        workoutOfTheDayViewController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Log WOD" image:[UIImage imageNamed:@"wod.png"] tag:1];
        scheduleViewController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Schedule" image:[UIImage imageNamed:@"schedule.png"] tag:2];
        trackingViewController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"Tracking" image:[UIImage imageNamed:@"tracking.png"] tag:3];


        self.viewControllers = [NSArray arrayWithObjects:navHome,navWorkOut,navSchedule,navTrack, nil];

    }
    return self;
}

create one separate class as UITabbarcontroller...

then inside of Appdelegate.h

self.window.rootViewController = [[MainTabBarController alloc]init];

Upvotes: 1

Related Questions