ios - 如何从 UIView 子类调用 UIViewController?

标签 ios objective-c iphone uitableview uiviewcontroller

我有主 UIViewController,其中有一个 UITableView

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

在 -

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

我正在调用 main 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] 进行了初始化。这总是返回空 View 。

你应该改为调用它

  • 如果你使用 Storyboard,给 View Controller 一个 Storyboard ID 并调用它

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

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

MainViewController *controller = [[MainViewController alloc] initWithNibName:@"Nib 文件名"bundle:nil];

关于ios - 如何从 UIView 子类调用 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37943640/

相关文章:

ios - UILocalNotification 操作未显示的操作

objective-c - ios 整数到 int 或 .5

objective-c - 如何在启动时确定应用程序是否在上次退出时崩溃?

objective-c - NSTextField 在不可编辑时停止调整大小

iphone - 谁能告诉我为什么 UIImage.ciimage 为空

iphone - 核心剧情动量滚动

ios - Cordova 应用程序的代码签名失败(使用 XCode 5.1.1 和 Cordova 3.4.1-0.1.0)

ios - 如何在iOS中使用AVPlayer达到4.0的播放速率?

iphone - 如何让 IBAction 在 iPad 应用程序启动时发生

iphone - iPhone NSTimer失效有问题