0

この問題がすでに投稿されている場合はすみません..

XCode 4.2 & iPhone SDK 5.0 を使用してプログラムで TabBar と NavigationBar の組み合わせを作成したい

期待どおりのビジュアルを生成します..しかし、TabBarItem を押して (テープで留めて) 対応するビューに変更すると、エラーが生成されます: [__NSCFString _tabBarItemClicked:]: 認識されないセレクターがインスタンスに送信されました

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

なぜそれが起こるのか、どのように回復するのかわかりません..誰か助けてください。

詳細が必要な場合は、ソースコードを提供できます...

前もって感謝します。

4

3 に答える 3

1

私が見る2つの問題...

  1. tabBarControllerのビューをウィンドウのとして割り当てsubViewます。これは正しくありません。rootViewControllerウィンドウの をに設定する必要がありますtBarController
  2. タブ バーのデリゲート メソッドを実装していますtabBar:didSelectViewController:か? tabBarエラー メッセージには、 に関連するメソッドをに送信しようとしていることが示されてい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 に答える