2012-09-02 57 views
1

我有一個tabBarView和一個GWDNativeViewController。 GWDNativeViewController是一個主菜單。我試圖讓該視圖首先顯示並超過tabBarView。我在IBAction中設置了相同的代碼,它可以工作。當我將它放入Loaddidfinishwithoptions時,它不起作用。iOS - 第二視圖不顯示在選項卡視圖上

@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]; 

} 
+0

secondView是否有視圖?你說這不起作用 - 你看到了什麼? – rdelmar

+0

我剛剛編輯帖子以包含工作代碼。當簡單地放置在viewDidLoad中時,除了顯示tabBarController外,它什麼也不做。然而,當用作IBAction時,相同的代碼有效。 – temmanuel

回答

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行。

+0

這是問題所在。謝謝! – temmanuel

0

tabBarController的視圖尚未載入此處。你可以在5個視圖控制器中的一箇中將該代碼放入viewDidLoad中。

+0

謝謝你的迴應。這是一個很好的想法。我試圖把它放在那裏(清理和重建),但它沒有讓它出現。 – temmanuel

+0

你還在試圖從你的標籤欄中顯示它嗎?你應該從其viewDidLoad所在的視圖控制器中呈現它。這應該起作用。 – thejav

相關問題