繁体   English   中英

如何从UIView子类调用UIViewController?

[英]How to call a UIViewController from a UIView SubClass?

我有主UIViewController ,其中有一个UITableView

UItableView的单元格中,我已经添加了UICollectionView

在-

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

我正在调用主UIViewController的方法。

UIViewController *view = [[UIViewController alloc] init];
[view my_method];

在主要观点上。

-(void)my_method {
    AnotherName *view_my=[[AnotherName alloc]initWithNibName:@"AnotherName" bundle:nil];
        [self.navigationController pushViewController:view_my animated:NO];
}

Viewdidload方法正在调用,但view未显示。

这是因为您通过调用[[UIViewController alloc] init] 这总是返回空视图。

你应该叫这个

  • 如果您使用情节提要,请为视图控制器提供一个情节提要ID,并将其命名为

MainViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"storyboard id"];

  • 如果您使用界面构建器,请调用此

MainViewController *controller = [[MainViewController alloc] initWithNibName:@"Nib File Name" bundle:nil];

我有解决办法..

AnotherName *view_my = [[AnotherName alloc]initWithNibName:@"AnotherName" bundle:nil];
self.window.rootViewController = view_my;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

特别感谢@The Mech System ..为了帮助我找到解决方案..现在它可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM