0

对不起,如果这个问题已经发布了..

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

它按预期产生视觉效果..但是当按下(录制) TabBarItem 以更改为其相应的视图时,它会产生错误:[__NSCFString _tabBarItemClicked:]: unrecognized selector sent to instance

这是AppDelegate的实现

#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

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

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

提前致谢。

4

3 回答 3

1

我看到的两个问题...

  1. 您将视图分配为tabBarController窗口的视图subView。这是不正确的。您需要将rootViewController窗口的 设置为tBarController
  2. 你在实现标签栏的委托方法tabBar:didSelectViewController:吗?您的错误消息说您正在尝试将tabBar-related 方法发送到NSString. 我怀疑这部分是由于第 (1) 点。尝试:

    self.window.rootViewController = self.tBarController;

于 2012-03-29T14:09:12.380 回答
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:

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

于 2012-03-29T13:52:15.510 回答
0

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

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

这只是一个例子……我认为这是正确的做法。

于 2012-03-29T15:01:19.830 回答