错误:以编程方式创建选项卡导航

时间:2012-03-29 13:43:01

标签: iphone objective-c ios tabbar navigationbar

如果已经发布此问题,请原谅..

我想使用XCode 4.2&amp ;;以编程方式创建TabBar和NavigationBar的组合。 iPhone SDK 5.0

它按预期产生视觉效果..但是当按下(录音)TabBarItem以更改为其相应的视图时,它产生错误: [__ NSCFString _tabBarItemClicked:]:无法识别的选择器发送到实例 < / p>

以下是 AppDelegat

的实现
#import "ApplicationDelegat.h"
#import "BrightnessController.h"


@implementation ApplicationDelegat

@synthesize window;
//@synthesize bControl;



- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    NSMutableArray *controllers = [NSMutableArray array];
    UITabBarController *tbarController = [[UITabBarController alloc] init];

    for (int i = 0; i <= 3; i++)
    {
        //self.bControl = [[BrightnessController alloc] initWithBrightness:i];
        BrightnessController *bControl = [[BrightnessController alloc] initWithBrightness:i];

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: /*self.bControl*/bControl];

        nav.navigationBar.barStyle = UIBarStyleBlackTranslucent;
        [controllers addObject: nav];
        //bControl.tabBarItem = [[UITabBarItem  alloc] initWithTitle:@"test" image:nil tag:i];
        //tbarController.navigationController.delegate = self;
    }

    tbarController.viewControllers = controllers;
    tbarController.customizableViewControllers = controllers;
    tbarController.selectedIndex = 0;
    tbarController.delegate = self;

   // NSCFString
    //tabBarItem

    // Set up the window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self.window addSubview:tbarController.view];
    [self.window makeKeyAndVisible];
}

@end

我不知道为什么会这样,以及如何恢复它.. 有人帮助我。

如果需要更多细节,我可以提供源代码......

提前致谢。

3 个答案:

答案 0 :(得分:1)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 7:20
switch(mytabbar.selectedIndex)
{
    case 0:
        [imageView1 setImage:[UIImage imageNamed:@"Tab1_sel.png"]];
        [imageView2 setImage:[UIImage imageNamed:@"Tab2.png"]];
        [imageView3 setImage:[UIImage imageNamed:@"Tab3.png"]];
        [imageView4 setImage:[UIImage imageNamed:@"Tab4.png"]];
        [imageView5 setImage:[UIImage imageNamed:@"Tab5.png"]];
        break;

    case 1:

您可以使用此方法并在每个标签栏上更改单击索引

答案 1 :(得分:1)

我看到两个问题......

  1. 您将tabBarController的视图指定为窗口的subView。这是不正确的。您需要将窗口的rootViewController设置为tBarController
  2. 您是否正在实施标签栏的委托方法tabBar:didSelectViewController:?您的错误消息显示您尝试向tabBar发送与NSString相关的方法。我怀疑它部分归因于第(1)点。尝试:

    self.window.rootViewController = self.tBarController;

答案 2 :(得分:0)

我通常更喜欢创建另一个tabbarview类来处理所有不同的视图控制器,在这种情况下,app delegate将如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];        
ViewController *viewController = [[ViewController alloc] init];
self.window.rootViewController = viewController.myTabBarController;
[self.window makeKeyAndVisible];
return;
}

在ViewController中我这样做:

标题文件:

@interface ViewController : UIViewController {

IBOutlet UITabBarController *myTabBarController;
}

@property (nonatomic, retain) IBOutlet UITabBarController *myTabBarController;

在实施文件中:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

    exerciseViewController *viewController1 = [[exerciseViewController alloc] init];
    viewController1.title = @"Exercise";
    viewController1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Exercise" image:[UIImage imageNamed:@"inbox.png"] tag:0];
    UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];        


    bookViewController *viewController2 = [[bookViewController alloc] init];
    viewController2.title = @"Book";
    viewController2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Book" image:[UIImage imageNamed:@"inbox.png"] tag:1];
    UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];


    askViewController *viewController3 = [[askViewController alloc] init];
    viewController3.title = @"Ask";
    viewController3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Ask" image:[UIImage imageNamed:@"inbox.png"] tag:2];
    UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:viewController3];


    workshopViewController *viewController4 = [[workshopViewController alloc] init];
    viewController4.title = @"Workshop";
    viewController4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Workshop" image:[UIImage imageNamed:@"inbox.png"] tag:3];
    UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:viewController4];

    myTabBarController = [[UITabBarController alloc] init];
    myTabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, thirdNavController, fourthNavController, nil];    


}
return self;
}

这只是一个例子......我认为这是正确的方法。