通用iOS 6应用程序的程序化故事板

时间:2013-02-17 15:24:31

标签: ios storyboard

更新1

Attributes inspector

更新1

更新0

Execution aborts when I implement the code suggested in the answer provided

更新0

This fine question and answer与我同时拥有iPhone和iPad xib并且现在想要使用故事板的Universal应用程序不太匹配。

这是基于xib的BSAppDelegate.m(在故事板之前)

    #import "BSAppDelegate.h"
    #import "BSViewController.h"

    @implementation BSAppDelegate

    NSString *receivedData;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        // return YES;
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController_iPhone" bundle:nil];
        } else {        
            self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController_iPad" bundle:nil];
    }
        self.window.rootViewController = self.viewController;   
        [self.window makeKeyAndVisible];
        return YES;
    }

我尝试在上面的else之后插入以下代码,但我无法完成需要设置self.viewController以与上述代码兼容的代码修复。

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"BSViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];

您能告诉我如何修复我的代码吗?我正在使用Xcode 4.5.2并为iOS 6开发。

1 个答案:

答案 0 :(得分:0)

两个问题:

  1. 只需将self.ViewController设置为从故事板中获取的vc:)

        #import "BSAppDelegate.h"
    #import "BSViewController.h"
    
    @implementation BSAppDelegate
    
    NSString *receivedData;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        // return YES;
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController_iPhone" bundle:nil];
        } else {        
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
            UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"BSViewController"];
            vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            self.viewController = (id)vc;
        }
        self.window.rootViewController = self.viewController;   
        [self.window makeKeyAndVisible];
        return YES;
    }   
    
  2. 在故事板中
  3. ,您没有设置名为“BSViewController”的标识符,因此调用instantiateViewControllerWithIdentifier将失败。设置屏幕截图中可见的标识符(又名故事板ID)