ios - 从另一个类呈现 View Controller

标签 ios objective-c uiviewcontroller

我正在尝试从该 View Controller 之外的类中呈现一个新的 View Controller 。

看起来像这样:

AppDelegate.m -> 里面有这段代码:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 

现在,该方法内部是一个使用另一个类的对象:[actionSheet launchActionSheetNav]; 这只是让操作表显示不同的选项。

现在,ActionSheets.m 中有一些代码,涉及如下的 segue:

handler:^(AHKActionSheet *as){

    ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
    [vc presentViewController:vc animated:YES completion:nil];
}];

我想做的就是从那里启动一个新的 View Controller ,但是执行 [self.navigationController 会带来 ActionSheets.m 不包含的错误这样的方法/属性。

如何从原始类之外呈现 View Controller ?

所以层次结构如下:

查看 Storyboard > 在 AppDelegate.m 上收听 > 在其中调用一个类方法,它将您带到 ActionSheets.m -> 从那里我需要显示新的 View Controller 。

最佳答案

这一行:

ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];

创建 ViewControllerYoutube 的新实例。这个 ViewController 除了那一行之外不存在于其他任何地方,所以当你跟随它时:

ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
[vc presentViewController:vc animated:YES completion:nil];

您正在尝试在尚未呈现的 View Controller 上呈现。

如果你想从外部类呈现,你需要一些方法来保持对你想要呈现的 View Controller 的引用,也许在你的 ActionSheet.h

@property (weak, nonatomic) ViewControllerYoutube *myViewControllerYoutube;

然后在创建操作表时分配它(假设您在 ViewControllerYoutube 中创建它)

ActionSheet * myActionSheet = [[ActionSheet alloc]init];
myActionSheet.myViewControllerYoutube = self;

然后代替这个:

ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
[vc presentViewController:vc animated:YES completion:nil];

调用这个:

[_myViewControllerYoutube presentViewController:vc animated:YES completion:nil];

更新

根据我们的聊天记录,我认为我们可以通过以下方式解决它。

  1. ActionSheet.h 中:

    @property (weak, nonatomic) UIViewController *presentingViewController;
    
  2. 在“ActionSheet.m”中

    ViewControllerYoutube *vc = [[ViewControllerYoutube alloc]init];
    [_presentingViewController presentViewController:vc animated:YES completion:nil];
    
  3. 在您的 AppDelegate 中:

    ActionSheet * actionSheet = [[ActionSheet alloc]init];
    UITabBarController * tabController = (UITabBarController *)self.window.rootViewController;
    actionSheet.presentingViewController = tabController.selectedViewController;
    

关于ios - 从另一个类呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643202/

相关文章:

iphone - 将音频流式传输到 iphone(带有播放/暂停选项)?

ios - 在 ToolbarItem SwiftUI 中添加带有图像和文本的按钮

iOS 动画主 UIViewController 的 View 高度

ios - 如何在自定义容器 View Controller 中实现交互式转换

ios - 将数据从 UIViewController1 传递到 UIViewController2 时 UIViewController2 的背景颜色消失

ios - 如何设置变量以从 Swift 中的 Firebase 数据库结构中获取值

ios - 如何在具有相同协议(protocol)变量的 Swift 中使用多个协议(protocol)?

ios - 带有参数的 NSMutableURLRequest - 无法解析响应

ios - Core Data中有类似sql "NOT IN"的函数吗?

ios - 单击或点击时 View Controller 中的元素消失