gpt4 book ai didi

ios - 如何在 ios 中从一个 View 导航到另一个 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:45 27 4
gpt4 key购买 nike

我有带有 4 个选项的标签栏。
在第一个选项卡中,我有一个按钮。当我单击此按钮时,我必须在第二个标签栏中从一个 View 移动到另一个 View 。

我当前使用的代码是(其中 shipVC 是第二个选项卡的 viewController):

[shipVC.navigationController pushViewController:cartVC animated:NO];

基本上,当按下第一个选项卡 viewController 中的按钮时,我想在第二个选项卡的 viewController 中从“X” View 移动到“Y” View

最佳答案

一个快速的方法是使用 NSNotificationCenter 在第一个选项卡的 viewController 中的按钮被按下时发布通知。

步骤:

  1. 在第二个选项卡的 viewController-viewDidLoad 方法中:
    • 添加通知观察者并为其设置目标选择器方法
  2. 在第一个选项卡的 viewController 的按钮方法中:
    • 发布通知

示例:

您的第二个选项卡的 viewController 类:

-(void)viewDidLoad {
[super viewDidLoad];

[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doTheNavigation)
name:@"AnyNameYouWantForNotification"
object:nil];
}

-(void)doTheNavigation {
[self.navigationController pushViewController:cartVC animated:NO];
//WARNING: don't just push, put a check or else repeated click on the
//button in the 1st tab will push cartVC again and again.
//This will not only warrant a crash but look very ugly
}

第一个选项卡的 viewController 类中的按钮方法:

-(IBAction)someBtnClick:(UIButton *)sender {
//...
//you can use this to post a notification from anywhere now
[[NSNotificationCenter defaultCenter] postNotificationName:@"AnyNameYouWantForNotification"
object:nil];
//...
}

所以...

  • 当第一个选项卡中的按钮被点击时,按钮操作(我命名为 someBtnClick)将发布一个名为 AnyNameYouWantForNotification
  • 第二个选项卡(应该已经加载并准备就绪)将监听名称为 AnyNameYouWantForNotification 的通知。
    • 收到此通知后,它将执行链接的选择器方法(我命名为doTheNavigation)

关于ios - 如何在 ios 中从一个 View 导航到另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769916/

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