我已經搜索了這個問題的答案,並會非常感謝一些幫助。我刪除了我的應用程序的其餘部分以簡化問題。按下按鈕上的視圖控制器點擊不起作用
我的根視圖只是一個按鈕的空白視圖。它已經初始化爲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];
這並不工作,但我覺得好像這是不是應該做的,任何想法呢?
藍色導航控制器出現在我的第一個視圖的頂部,顯示它在那裏,但只是沒有響應! –
你有沒有嘗試在你的'switchView:'方法中放入'NSLog(@「Button pressed!」)'來查看它是否被調用? –