2010-11-09 106 views
3

正如你猜測我還是一個新手,讓我的腦袋圍繞iphone開發。 我只是嘗試了基本視圖加載點播,我無法上班在沒有導航控制器的情況下導航到另一個視圖控制器

我有一個應用程序,2視圖控制器,每個視圖控制器連接不同的筆尖文件。 我試圖手動切換視圖;不涉及導航控制。

我該如何手動將第二個視圖推送到第一個視圖? self.navigationController pushViewController不會工作,因爲沒有導航控制器。 我還可以如何推動第一個視圖上的第二個視圖並銷燬第一個視圖;反之亦然? 我在第一種觀點的按鈕動作做到了這一點:顯然

SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; 
[self.navigationController pushViewController:sv animated:YES]; 

,它沒有工作。

window addSubView也沒有工作,因爲第一個視圖控制器是根視圖控制器(不知道我說的是正確的)。換句話說,當我運行應用程序時,第一個視圖是我看到的應該加載第二個視圖的按鈕。

我花了幾個小時尋找一個簡單的例子,我找不到任何。

有什麼建議嗎?

在第一視圖控制器
+0

SecondView * sv = [[SecondVie w alloc] initWithNibName:@「SecondView」bundle:nil]; \t [self.view addSubview:sv.view]; \t [sv release];似乎工作,但沒有動畫;我如何添加動畫就像它發生在導航控制器 – Veeru 2010-11-09 09:43:03

+0

編輯我的答案。 :)包括動畫 – 2010-11-09 10:15:47

回答

10

你需要這個:

- (IBAction)pushWithoutViewController:(id)selector { 
    NextNavigationController *page = [[NextNavigationController alloc] initWithNibName:NextNavigationController bundle:nil]; 
    CGRect theFrame = page.view.frame; 
    theFrame.origin = CGPointMake(self.view.frame.size.width, 0); 
    page.view.frame = theFrame; 
    theFrame.origin = CGPointMake(0,0); 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.8f]; 
    page.view.frame = theFrame; 
    [UIView commitAnimations]; 
    [self.view addSubview:page.view]; 
    [page release]; 
} 

,然後將其在筆尖鏈接到該按鈕。 :)

+0

偉大的,吉亞尼和托馬斯,你的解決方案工作! Gyani的解決方案只給出一個動畫,將其向上滑動,在presentModalViewController中是否有任何選項,我可以從右向左設置動畫?托馬斯的解決方案更精細,我可以控制動畫以任何方式和速度我想要的。 – Veeru 2010-11-10 01:52:45

+0

此解決方案僅替換視圖,而不是視圖控制器。另外,initWithNibName接受第一個參數作爲NSString,而不是class。因此,它應該被寫爲... initWithNibName:@「NextNavigationController」... – Raptor 2011-05-11 04:30:56

+0

啊,發現在initWithNibName前面...忘了我的@「」...:p你永遠不會「替換」一個視圖控制器。它只是一個對象。你所做的只是擁有物體,而不是將其交給導航控制器來控制。 – 2011-05-11 09:24:38

8

嘗試:

SecondView *sv=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; 
[self presentModalViewController:sv animated:YES]; 
+0

嗨gyani,是否有可能用相同的語句添加不同類型的動畫? – Veeru 2010-11-10 02:30:12

+2

@Veeru是的,可以添加不同類型的動畫 例如 SecondView * sv = [[SecondView alloc] initWithNibName:@「SecondView」bundle:nil]; sv.modalTransitionStyle = UIModalTransitionStyleFlipHorizo​​ntal; [self presentModalViewController:sv animated:YES]; – Gyani 2010-11-10 04:08:27

+0

感謝一羣gyani和托馬斯以及。由於涉及的代碼較少,我很喜歡gyani的方法。再次感謝 :) – Veeru 2010-11-10 07:16:21

0

如果您已經廈門國際銀行,你想給導航到另一個控制器,那麼你必須在申報導航appdelegate.m 寫下面的代碼到App

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
self.window.rootViewController = navController; 

然後在ViewController.m

- (IBAction)NextButtonClicked:(id)sender 
{ 
    yourNextViewController *objyourNextViewController = [[yourNextViewController alloc] initWithNibName:@"yourNextViewController" bundle:nil]; 
    [self.navigationController pushViewController:objStartUpViewController animated:TRUE]; 
} 
相關問題