0

私は Objective-C を勉強しており、学習中にアプリを作成し始めました。「ViewController」と「secondViewController」という 2 つの「View Controller」を持つ「Single View Application」を作成しました。その後、「UITabBarController」を追加することを考えたので、チュートリアルに従って、AppDelegate.m で次のコードを使用しました。

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[self customizeInterface];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UITabBarController *tabController = [[UITabBarController alloc] init];

UIViewController *viewController1 = [[UIViewController alloc] init];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Artists" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];
[viewController1 setTabBarItem:tab1];

UIViewController *viewController2 = [[UIViewController alloc] init];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Music" image:[UIImage imageNamed:@"music-tab.png"] tag:2];
[viewController2 setTabBarItem:tab2];

tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
                                 viewController2, nil];


self.window.rootViewController = tabController;

[self.window makeKeyAndVisible];


// Override point for customization after application launch.
return YES;
}

ここにViewController.mがあります

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

アプリケーションを実行すると、タブ バーは機能しますが、作成した ViewController ではなく空白の画面が表示されます。私は初心者なので、各タブでそれぞれView Controllerをリンクまたは表示する方法を教えてください。

当たり前のことも全部言ってくださいますので、よろしくお願いします。前もって感謝します

4

2 に答える 2

1

初心者なので、ストーリーボードを使用することを強くお勧めします。ほとんど何もコーディングする必要がないため、インターフェイスの管理がはるかに簡単になります。

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

私は推測しているだけですが、あなたの問題はあなたが直接initあなたのビューコントローラであるかもしれません、あなたはinitWithNibName:あなたのでメソッドを呼び出すべきdidFinishLaunchingWithOptionsです。これinitWithNibName:は、nibファイルからコントローラインスタンスを作成するときに呼び出すものです。

したがって、基本的にinitWithNibName:は、NIBがロードされてインスタンス化されるときに呼び出されます。

ViewContollerクラス名はわかりませんが、ViewControllerのinitメソッドを変更する必要があります

アプリのdelegate.mで

#import "ViewController.h"
#import "secondViewController.h" 


    UIViewController *viewController1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]]; // make sure on interface builder hat you Nib name is ViewController

    UIViewController *viewController2 = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:[NSBundle mainBundle]];// make sure on interface builder hat you Nib name is secondViewController

クラス名の大文字と詳細な名前を使用してくださいsecondViewControllerはプログラミングの練習としては適切ではありません

于 2013-01-31T17:41:10.283 に答える
0

タブバー コントローラーは、コードで作成したビューを表示します。まったく新しいビューを作成しているため、これは予想される動作です。

Interface Builder で「ViewController」と「secondViewController」を作成した場合は、新しいビューを作成する代わりに nib をロードし、これらを使用するようにタブバー コントローラーに指示する必要があります。

基本的に、行を変更する必要があります。

UIViewController *viewController1 = [[UIViewController alloc] init];

のようなものに

UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"view1" bundle:nil];

詳しくはこちらをご覧くださいinitWithNibName:bundle:

于 2013-01-31T17:45:21.717 に答える