ios – 如果没有导航栏,如何在EKEventViewController中获得“完成”或“后退”按钮?

我的iOS应用程序中有一个日历事件列表,单击时可以在EKEventViewController中打开.这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EKEventViewController *eventViewController = [EKEventViewController new];
    eventViewController.event = [self.events objectAtIndex:[indexPath row]];
    [self presentViewController:eventViewController animated:YES completion:nil];
}

事件视图正确地从屏幕底部弹出,但我无法返回事件列表!

我正在使用导航控制器(但没有导航栏!),所以添加此代码使我能够返回列表:

-(void)viewDidAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EKEventViewController *eventViewController = [EKEventViewController new];
    eventViewController.event = [self.events objectAtIndex:[indexPath row]];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController pushViewController:eventViewController animated:YES];
}

但是,这种解决方案是不是很优雅,因为当我按在事件视图中的“后退”按钮,不除导航栏(它显示在我的事件列表视图顶部)事件视图之前所有画面之外.

如何解决这个问题?最好的办法会一直以某种方式得到的,我可以在这里的第一个代码使用的事件视图中的后退按钮(所以我会避免显示导航栏),并认为可能只是向后滑动到屏幕底部按下时

解:
结束以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EKEventViewController *eventViewController = [EKEventViewController new];
    eventViewController.event = [self.events objectAtIndex:[indexPath row]];
    eventViewController.delegate = self;
    UINavigationController *navBar = [[UINavigationController new] initWithRootViewController:eventViewController];
    [self presentViewController:navBar animated:YES completion:nil];
}

- (void)eventViewController:(EKEventViewController *)controller didCompleteWithAction:(EKEventViewAction)action
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

最佳答案 看起来你根本不需要隐藏导航栏!你能解释一下你需要隐藏或显示导航栏的更多原因吗?这将是隐藏或显示模型视图的简单解决方案.

// EKEventListVC.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
            EKEventViewController *pms = [EKEventViewController new];
            UINavigationController *nav = [[UINavigationController alloc]
                                           initWithRootViewController:pms];
            [self presentViewController:nav animated:YES completion:nil];
}

// EKEventViewController.m

- (void)addRightButton
{
    UIButton *rightButton = // make your button
    [rightButton addTarget:self action:@selector(rightBtnActionDone:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    [self.navigationItem setRightBarButtonItem:barButtonItem];
}

//

- (void)rightBtnActionDone:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//为EKEventKitUI编辑

-(void)eventEditViewController:(EKEventEditViewController *)controller
         didCompleteWithAction:(EKEventEditViewAction)action {

    switch (action) {
       case EKEventEditViewActionCanceled:
           // User tapped "cancel"
           break;
       case EKEventEditViewActionSaved:
           // User tapped "save"
           break;
       case EKEventEditViewActionDeleted:
           // User tapped "delete"
           break;
       default:
           break;
    }

    [self dismissModalViewControllerAnimated:YES];
}

Code Example

点赞