2011-09-14 105 views
1

我嘗試使用不同視圖創建應用。每個視圖控制器都有兩個視圖。第一個視圖通過AppDelegate類中的rootViewController加載。現在我想加載另一個視圖作爲點擊按鈕的結果。在iOS中查看視圖切換

我試圖在我的第一個控制器中創建第二個視圖控制器的實例,並使用pushViewController方法,但它沒有奏效。該項目已建成,但點擊按鈕後沒有任何事情發生。

+0

你是如何instanciate第二個視圖控制器?你可以發佈有關代碼嗎? – 2011-09-14 17:17:38

+0

@vincent您需要在您要推送的視圖控制器類上分配和initWithNibName。這個答案有一些好點:http://stackoverflow.com/questions/1644293/how-to-push-viewcontroller-view-controller – nykash

+0

@nykash - 謝謝,但我已經知道這一點? – 2011-09-14 18:53:24

回答

1

更新我的回答:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
PasswordHandling *aPasswortHandler = [[PasswordHandling alloc] init]; 
self.passwordHandling = aPasswortHandler; [aPasswortHandler release]; 
UINavigationController *navigationController; 
if ([passwordHandling passwordInKeyChain] == TRUE) { 
    LogInView *logInView = [[[LogInView alloc] initWithNibName: @"Log In View" bundle: nil] autorelease]; 
    navigationController = [[UINavigationController alloc] initWithRootViewController:logInView]; 
    //[navigationController pushViewController: logInView animated:NO]; 
} else { 
    CreateNewPasswordView *createNewPasswordView = [[CreateNewPasswordView alloc] initWithNibName:@"CreateNewPassword" bundle: nil]; 
    navigationController = [[UINavigationController alloc] initWithRootViewController:createNewPasswordView]; 
    //[navigationController pushViewController: createNewPasswordView animated:NO]; 
} 

[self.window addSubview:navigationController.view]; 
[self.window makeKeyAndVisible]; return YES; 
+0

這是我的AppDelegate代碼。 PasswortHandler是一個通過keychain' - (BOOL)應用程序通信的類:(UIApplication *)application didFinishLaunchingWithOptions :(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]自動釋放]; PasswordHandling * aPasswortHandler = [[PasswordHandling alloc] init]; self.passwordHandling = aPasswortHandler; [aPasswortHandler發佈]; if([passwordHandling passwordInKeyChain] == TRUE){ //密鑰鏈中的密碼 –

+0

LogInView * logInView = [[[LogInView alloc] initWithNibName:@「Log In View」bundle:nil] autorelease]; [navigationController pushViewController:logInView animated:NO]; } else { CreateNewPasswordView * createNewPasswordView = [[CreateNewPasswordView alloc] initWithNibName:@「CreateNewPassword」bundle:nil]; [navigationController pushViewController:createNewPasswordView animated:NO]; } [self.window setRootViewController:navigationController]; [self.window makeKeyAndVisible]; 返回YES; }啓動iPhone屏幕後保持白色。怎麼了? –

+0

看不到所有代碼。你可以通過添加代碼來更新你的問題嗎? - 忽略此評論,因爲我無法刪除它。 – Saran

0

您必須將您的第一個viewController放在UINavigationController中,並將此導航控制器設置爲rootViewController。然後當您點擊此按鈕,通話(假設你在你的第一個控制器是):

[self.navigationController pushViewController:secondViewController animated:YES]; 
0

薩蘭和Zoleas雙雙創下的臨界點......但這可能有助於看到幾個不同的例子。請參見本作的解釋(我通常使用的方法#2我的應用程序)

How to push viewcontroller

0

檢查,以確保您的視圖的按鈕選擇正確有其目標/行動設定由右/控制點擊按鈕查看其連接彈出。