2014-02-11 69 views
0

我在viewcontroller1,我想去viewcontroller2,在1我做的:viewControllers之間切換手動

UIViewController *sv=[[signUpView alloc]init]; 
[self addChildViewController:sv]; 

哪裏signUpView是新的viewController class(的view controller子類) 沒有什麼反應。

我試過:[self.view removeFromSuperview]; 哪些工作,並刪除我的看法。

  1. 我該怎麼做過渡到2
  2. 如何在1加2比回到1?

謝謝。

+0

爲什麼你不使用pushviewcontroller或presentviewcontroller。爲了返回到V1,你會得到一個後退按鈕(pushviewcontroller)。或使用presentviewcontroller解除視圖控制器。 – Pawan

回答

0

的一般方法是:

[ParentViewController.view addSubview: ChildViewController.view]; 
    [ParentViewController addChildViewController: ChildViewController]; 
    [ChildViewController didMoveToParentViewController: ParentViewController]; 

在你的情況,這將是:

[self.view addSubview: sv.view]; 
    [self addChildViewController: sv]; 
    [sv didMoveToParentViewController: self]; 

也可參閱Apple.com的View Controller Containment文章。

1

替換爲您的代碼:

UIViewController *sv=[[signUpView alloc]init]; 
[self addChildViewController:sv]; 
[self.view addSubview:sv.view]; 
[sv didMoveToParentViewController:self]; 

我不把任何評論,因爲這個代碼是自我解釋。 這應該有所幫助。

0

另一種選擇是使用segues在您的Storyboard中設置您的UIViewController交互,然後以編程方式調用segue。如果你不確定如何設置一個segue,那麼看看Ray Wenderlich的this tutorial或搜索一個;那裏有很多,我寫的東西會比較蒼白。

由於所有內容都是在故事板中配置的,所以代碼很簡單。這是我的viewController1撥打電話切換到viewController2的示例。

[self performSegueWithIdentifier: @"DoNumber2" sender:self];