gpt4 book ai didi

ios - 从另一个类呈现 View Controller

转载 作者:行者123 更新时间:2023-11-29 10:42:54 24 4
gpt4 key购买 nike

我正在尝试从该 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com