如何关闭选项卡式视图控制器

时间:2016-08-11 04:59:48

标签: ios objective-c uiviewcontroller uitabbarcontroller

我解决了选项卡式视图控制器的问题,可以帮助一些人。

现在进入处理步骤。

  1. 我的第一个viewcontroller如下图所示。 when I click any button I presenting the tabview controller

    2.当我点击任何显示tabview控制器的按钮时 enter image description here

  2. 3.当我进入相机标签时,tabedview如下所示 enter image description here

    4.当我按下一个时,我正在呈现视图控制器 enter image description here

    5.这个viewcontroller当我按下Share按钮时,我们必须关闭到tabbeded viewcontroller,即tabeedview controller.show中的1sttab,如下图enter image description here

    等待快速回应,任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:0)

我有一个解决您问题的方法。如果使用模态显示视图,请尝试使用此代码关闭视图控制器: //夫特

self.dismissViewControllerAnimated(true, completion: {});

//目标C

[self dismissViewControllerAnimated:YES completion:nil];

在完成处理程序中执行类似这样的操作来更改选项卡栏索引:

NSInteger indexOfTabToSwitchTo = 1//Change this number according to your preference of controller
[self.presentingViewController.tabBarController setSelectedIndex:indexOfTabToSwitchTo];

/。结合在一起可能会看起来像这样

[self dismissViewControllerAnimated:YES completion:^{
    NSInteger indexOfTabToSwitchTo = 1//Change this number according to your preference of controller
    [self.presentingViewController.tabBarController setSelectedIndex:indexOfTabToSwitchTo];
}];

答案 1 :(得分:0)

您可以使用标签栏控制器的selectedIndex属性来解决您的问题

// Create button in `ViewDidLoad` and add to tableview.
- (void)viewDidLoad
{
    [super viewDidLoad];
    // *** Creat a button, calculate Y position at bottom of table, assign frame & add to tableview ***
    _btnFloating = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat yPos = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);
    [_btnFloating setFrame:CGRectMake(0, yPos, [UIScreen mainScreen].bounds.size.width, 50)];
    [self.tableView addSubview:_btnFloating];

    // *** Adjust Inset and scroll indicator of tableview ***
    self.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);

    // *** Add observer on tableview frame change to update floating button frame ***
    [self.tableView addObserver:self
                     forKeyPath:@"frame"
                        options:0
                        context:NULL];
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        [self adjustFloatingViewFrame];
    }
}

- (void)adjustFloatingViewFrame
{
    CGRect newFrame = _btnFloating.frame;

    newFrame.origin.x = 0;
    newFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);

    _btnFloating.frame = newFrame;

    [self.tableView bringSubviewToFront:_btnFloating];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self adjustFloatingViewFrame];
}

// Don't forget to remove Observer on Tableview otherwise it will lead to crash.
-(void)dealloc
{
    [self.tableView removeObserver:self forKeyPath:@"frame"];
}

-

试试这个

[self.tabBarController setSelectedIndex:0];

答案 2 :(得分:0)

您可以使用此代码:

[self dismissViewControllerAnimated:YES completion:^{
     [self.tabBarController setSelectedIndex:0];   
    }];