iOS - 第二个视图未显示在选项卡视图上

时间:2012-09-02 01:08:51

标签: ios views tabbar

我有一个tabBarView和一个GWDNativeViewController。 GWDNativeViewController是一个主菜单。我试图让该视图首先显示并超越tabBarView。我在IBAction中有相同的代码集,它可以工作。当我把它放入Loaddidfinishwithoptions时,它不起作用。

@implementation GWDNativeAppDelegate
@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
@synthesize secondView = _secondView;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.
    UIViewController *viewController1 = [[GWDNativeFirstViewController alloc] initWithNibName:@"GWDNativeFirstViewController" bundle:nil];
    UIViewController *viewController2 = [[GWDNativeSecondViewController alloc] initWithNibName:@"GWDNativeSecondViewController" bundle:nil];
    UIViewController *viewController3 = [[GWDNativeThirdViewController alloc] initWithNibName:@"GWDNativeThirdViewController" bundle:nil];
    UIViewController *viewController4 = [[GWDNativeFourthViewController alloc] initWithNibName:@"GWDNativeFourthViewController" bundle:nil];
    UIViewController *viewController5 = [[GWDNativeFifthViewController alloc] initWithNibName:@"GWDNativeFifthViewController" bundle:nil];

    //tabBarController Stuff
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, viewController5, nil];
    self.window.rootViewController = self.tabBarController;

    //Specify W`enter code here`hich tab to display (Number need to be set based on button selected)
    //self.tabBarController.selectedIndex = 2;       

    GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
    [secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self.tabBarController presentModalViewController:secondView animated:YES];

    [self.window makeKeyAndVisible];
    return YES;
}

这是GWDNativeFirstViewController.m中的代码 IBAction中的代码有效.. viewDidLoad中的代码没有。?

-(IBAction)pressedButton {

    GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
    [secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentModalViewController:secondView animated:YES];
    //[secondView release];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];

    }
    return self;
}

- (void)viewDidLoad
{
    [contentWebView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.web.org/?iapp=1#tab1"]]];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

     GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
     [secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
     [self.tabBarController presentModalViewController:secondView animated:YES];

}

2 个答案:

答案 0 :(得分:0)

tabBarController的视图尚未加载到此处。您可以将该代码放入5个视图控制器之一的viewDidLoad中。

答案 1 :(得分:0)

我不确定为什么它在viewDidLoad中不起作用,但它可以在applicationDidFinishLaunchingWithOptions中工作:如果在定义secondView之前放置[self.window makeKeyAndVisible]行。

[self.window makeKeyAndVisible];
 GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
 [secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
 [self.tabBarController presentModalViewController:secondView animated:YES];

如果您希望它立即显示,请将动画选项更改为NO,并删除定义modalTransitionStyle的行。