2011-11-18 72 views
1

我已經搜索了這個問題的答案,並會非常感謝一些幫助。我刪除了我的應用程序的其餘部分以簡化問題。按下按鈕上的視圖控制器點擊不起作用

我的根視圖只是一個按鈕的空白視圖。它已經初始化爲UINaviagationController。我想要做的就是在按下按鈕時將按鈕推入新的視圖。目前,按下按鈕時什麼也不做,這是我的代碼。

#import <UIKit/UIKit.h> 

@class ViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) ViewController *viewController; 

@end 


@implementation AppDelegate 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 

    [self.window addSubview:navigationController.view]; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

然後在我的第一個視圖控制器:

#import "ViewController.h" 

@implementation ViewController 

-(IBAction)switchView:(id)sender 
{ 
View2 *v2 = [[View2 alloc] initWithNibName:@"View2" bundle:nil]; 

[self.navigationController pushViewController:v2 animated:YES]; 

} 

的 - (IBAction爲)配置正確,但仍沒有任何想法?

編輯::

我找到了解決辦法。

看來,當試圖從根視圖訪問NavigationController時,使用[self.navigationController ....]不會將消息發送到navigationController的實例(不知道爲什麼!)。

我知道了通過添加的ivar到的ViewController類型的UINavigationController的工作,然後初始化的ViewController,像這樣之後設置從AppDelegate中的實例變量:在ViewController中發送消息到NavigationController時

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 


    //The UINavigationController declared int AppDelegate.h 
    navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 

    [self.window addSubview:navController.view]; 

    //Set the ivar on ViewController (navCon) to the UINavigationController we just 
    //initialized 
    self.viewController.navCon = navController; 

查閱。男,我用的線

View2 *v2 = [[View2 alloc] initWithNibName:@"View2" bundle:nil]; 

[navCon pushViewController:v2 animated:YES]; 

這並不工作,但我覺得好像這是不是應該做的,任何想法呢?

+0

藍色導航控制器出現在我的第一個視圖的頂部,顯示它在那裏,但只是沒有響應! –

+2

你有沒有嘗試在你的'switchView:'方法中放入'NSLog(@「Button pressed!」)'來查看它是否被調用? –

回答

3

你的代碼在application: didFinishLaunchingWithOptions:是相當混亂。您必須將導航控制器設置爲窗口的rootViewController,而不是導航控制器的根視圖控制器。然後,行[self.window addSubview:navigationController.view];是多餘的,應該刪除。

+0

Spot on mate!謝謝 –

0

我不確定這是否可行,它可能只適用於coocs2d。但請嘗試在實施的開始階段。

self.isTouchEnabled = YES; 
+0

不應該需要,它默認爲yes。 –

0

可能有些事情可能會有問題。

  • 您沒有連接IB中的所有內容。
  • View2類不是UIViewController的一個子類,但一個UIView的(很難在這一點上說)

我會去了解一下這是IB建立的第一個問題。確保您的touchUpInside動作連接器已連接到IB文件中的UIViewController對象中的相應功能switchView

+0

嗨Cyprian,謝謝你的迴應。我已經得到它的工作,問題似乎是從根視圖的'[self.navigationController ...]'實際上並沒有發送消息到navigationController實例。我不得不給ViewController一個UINavigationController ivar,然後將ivar設置爲在AppDelegate中創建的navigationController實例,之後的視圖看起來很好,爲什麼會有這種想法? –

+0

請參閱Ole Begemann所說的,它以這種方式使用UINavigationController的錯誤方法。您可能應該使用xcode模板之一來啓動基於導航的應用程序,並從中看到它是如何工作的 – Cyprian