问题
I have a list of calendar events in my iOS app, that is to be opened in an EKEventViewController when clicked. Here is my code:
- (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];
}
The Event view correctly pops up from the bottom of the screen, but I have no way of going back to the list of events!
I am using a navigation controller (but no navigation bar!), so adding this code made me able to go back to the list:
-(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];
}
But this solution is not very elegant, because when I press the "Back" button in the event view, the navigation bar is not removed (it shows on top of my event list view) before the event view is all out of the screen.
How to fix this? The best option would have been to somehow got a back button in the event view that I could have used with the first code here (so I would avoid showing the navigation bar), and the view could just slide back to the bottom of the screen when pressed.
SOLUTION: Ended up with the following code:
- (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];
}
回答1:
Looks like you do not need to hide the navigation bar at all ! Can you explain more why you need to hide or show the nav bar ? This will be simple solution to hide or show the model view.
// 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];
}
// Edited For 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 -
回答2:
I would keep the navigation bar in both cases as it is the best place to put a back/close button in a controller with a view hierarchy that you don't control.
a) Modally:
- Adjust the
EKEventViewController
navigationItem
and embed the controller inside aUINavigationController
. Then present the later!
b) Push:
- Do not hide the navigation bar and let the automatic back button appear. If you want to have a different UI for a particular controller a) is the better way to go.
If you do keep the navigation bar on your presented controller you can add a dismiss button like this:
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(dismissController:)];
The just implement dismissController:
in your presenting controller.
来源:https://stackoverflow.com/questions/23698060/how-to-get-a-done-or-back-button-in-an-ekeventviewcontroller-when-no-having