2017-01-06 102 views
0

我想從UIScrollView子視圖呈現視圖控制器。 我試過使用AppDelegate window.rootViewController presentViewController:但是這給了我「視圖不在層次結構!」錯誤。 我想避免使用addSubview,因爲它打破了MVC,似乎刪除了控制器的功能(按鈕停止工作)。 當我使用期望的presentViewController方法時,我得到「沒有可見的@interface爲」InititalScrollViewSubview「聲明選擇器」presentViewController:animated:completion:「,我認爲這意味着我的initialScrollViewSubview試圖使用presentViewController,但presentViewController必須來自。一個UIViewController的UIScrollView是沒有presentViewController方法從UIScrollView子視圖呈現故事板UIViewController

我的代碼是一樣的東西:

-(void)setupTouchIDButtonTapped: (id)sender { 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"newViewController" bundle:nil]; 
    NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:@"myNewVC"]; 

//第一件事我想:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
[delegate.window.rootViewController presentViewController:NewViewController animated:YES completion:nil]; 

//第二件事情,我想我試圖

[self addSubview:NewViewController.view]; 

//第三件事:

[self presentViewController:NewViewController animated:YES completion:nil]; 

} 

的InitialScrollViewSubview必須保持它的方式。理想情況下,我會重構一切,以便InitialScrollViewSubview是另一個UIViewController,但我爲一家大公司工作,而且該應用程序太大了:) 任何建議都非常感謝!

謝謝!

回答

0

如果您嘗試在視圖控制器的視圖中嵌入視圖控制器的視圖,那麼您不會呈現視圖控制器。您實例化它,然後將其視圖添加到視圖層次結構中。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"newViewController" bundle:nil]; 
    NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:@"myNewVC"]; 

[scrollView addSubview:NewViewController.view]; 

您還需要設置視圖的大小/框架或設置自動佈局約束,以便視圖控制器的大小正確。

+0

謝謝! 我試過了,使用[self addSubview:NewViewController.vew]; 視圖出現,但我所有的按鈕停止工作。這是因爲它失去了「控制器」功能,因爲它現在只是scrollView視圖的一部分? –

+0

scrollview可能是註冊爲接收事件的主視圖,請嘗試在viewcontroller.view上啓用userInteractionenabled。你也可以嘗試這裏的提示:http://stackoverflow.com/questions/4629499/how-to-steal-touches-from-uiscrollview – Scriptable

+0

這可能是你需要的解決方案http://stackoverflow.com/questions/16649639/UIButton的 - 不 - 不工作時,其功能於UIScrollView中 – Scriptable

0

嘗試此

UIStoryboard *故事板= [UIStoryboard storyboardWithName:@ 「newViewController」 束:無];

NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:@"myNewVC"]; 

AppDelegate * delegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];

UINavigationController * rootNvc = delegate.window.rootViewController;

[rootNvc pushViewController:NewViewController animated:YES];