【问题标题】:UITabBarController as RootViewController in MonoTouchUITabBarController 作为 MonoTouch 中的 RootViewController
【发布时间】:2012-10-28 14:52:11
【问题描述】:

我有一个带有 UITabBarController 的 HomeScreen,我想将其设置为 RootViewController,但是当我这样做时出现以下错误

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

我还设置了与文件所有者相同的 XIB 名称的 Nib 名称。

这是我的项目示例。 Click Here to Download Project

【问题讨论】:

    标签: iphone mono xamarin.ios uitabbarcontroller xib


    【解决方案1】:
    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;
    }
    

    MainTabBarController 如下所示..

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

    更多信息见this answer

    【讨论】:

    • 嘿,看看答案,其中创建带有一堆视图的标签栏,然后添加为窗口的子视图..
    【解决方案2】:

    您必须以编程方式设置 Viewcontrollers....如下所示

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

    创建一个单独的类作为UITabbarcontroller...

    然后在Appdelegate.h内部

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

    【讨论】:

    • 问题不在于没有设置视图控制器。该类不“兼容”。根据日志,问题是“此类不符合键 tabBarController 的键值编码”。因此需要设置根视图控制器。
    猜你喜欢
    • 2013-10-27
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多