【问题标题】:How to change a tabbar after performing a segue transition执行转场转换后如何更改标签栏
【发布时间】:2016-02-25 08:45:20
【问题描述】:

我遇到了问题。在尝试转场(实际上是展开转场)之后,我想以编程方式移动到另一个选项卡(用户从前面的选项卡开始一个操作,被转接到模态视图并执行他/她的任务,完成模态视图后展开,然后我想给用户反馈并在最后一个选项卡中显示它(以便用户可以在此之后开始新操作或稍后再次返回反馈)。

@IBAction func backToSimulatorView(segue:UIStoryboardSegue) { self.ToFeedback(segue) }

当我在同一个视图控制器中添加一个按钮并执行此操作时,它工作得很好:

@IBAction func ToFeedback(sender: AnyObject) {
  let tc = self.tabBarController! as UITabBarController
  tc.selectedIndex = 2
}

此代码不起作用:

@IBAction func backToSimulatorView(segue:UIStoryboardSegue) { self.ToFeedback(segue) let tc = self.tabBarController! as UITabBarController tc.selectedIndex = 2 }

我可能可以使用通知来解决它,但感觉有点像 hack。

【问题讨论】:

  • 你可以有一个回调或委托模式。您必须通过提供后退导航实现来挂钩导航栏中的后退操作
  • 感谢帕布。你帮我解决了问题。我认为我最初策略的问题是事件的顺序与我预期的不一样。索引可能会更改,但在重置为索引 0 之后会立即更改,因为展开到该初始视图。我现在已经在 viewController viewWillAppear 方法中实现了一个检查,如果 bool 为真,它将移动到另一个选项卡:所以是一种回调。

标签: swift2 ios9


【解决方案1】:

这里是回调机制,在FirstViewController.m中

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //object ref, we need to set the callback property of destination view controller
    SecondViewController *svc = (SecondViewController*)[segue destinationViewController];

    [svc setMyCallBack:^(NSString *dat) {
        NSLog(@"%@", dat); //Prints the data posted back by Secondviewcontroller.
    }];
}

在 SecondViewController.h 中

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

- (void)setMyCallBack:(MyNewCallBack)cb; //First view controller sets the callback through this method.

@end

在 SecondViewController.m 中

@interface SecondViewController ()
@property (nonatomic, strong)MyNewCallBack mycb;// we need this property to access the callback where ever we need.
@end

- (IBAction)NavigationBarBackAction:(id)sender {
    self.mycb(@"MyData"); // Here i am passing a string to First View Controller.
}

- (void)setMyCallBack:(MyNewCallBack)cb {
    self.mycb = cb;
}

希望对你有帮助

【讨论】:

  • 感谢机制,虽然是 Objective C,但我使用的是 Swift 2.1。
  • 你知道如何将 Obj C 转换为 swift 吗?
  • 你好。该问题被标记为“swift”,请以 Swift 而非其他语言提供答案。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-05-24
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2012-09-11
  • 2011-06-19
相关资源
最近更新 更多