Johnny
Johnny

Reputation: 851

Objective C UIViewController Delegate

I have a UIViewController to which I've added a label, a button, a tableView and a searchView. I call this locationTableView in my system

I then have another viewController called mapTabBarController which when the user clicks on a button I call this code

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES];    
locationTableView = [self.storyboard instantiateViewControllerWithIdentifier:@"LocationTableViewController"];
[self.view addSubview:locationTableView.view];

I disable the status bar and the navigation bar as the location view is a direct overlay on the screen but still displays our mapTabBar

I am working with the cancel button currently, which really just hides the locationTableView and turns the navigationbar and the status bar back to being displayed. I originally tried to get this working just the standard way of having

- (IBAction)cancelButtonPressed:(id)sender {
    [[UIApplication sharedApplication] setStatusBarHidden: NO];
    [self.view removeFomSuperview];
}

This worked, but then I couldn't figure out how to get the navigationBar to re-display. So I thought of creating a delegate to do it, so I began working on that:

LocationTableViewController.h

#import <UIKit/UIKit.h>
@class LocationTableViewController;
@protocol LocationTableViewControllerDelegate;

@interface LocationTableViewController : UIViewController

    @property (weak, nonatomic) IBOutlet UITableView *locationTableView;
    - (IBAction)cancelButtonPressed:(id) sender;
    @property (strong, nonatomic) id<LocationTableViewControllerDelegate>delegate;
@end

@protocol LocationTableViewControllerDelegate <NSObject>

- (void) LocationTableViewcontroller:(LocationTableViewController *)viewController cancelButtonPressed:(CGFloat)value;

@end

LocationTableViewController.m

#import "LocationTableViewController.h"

@interface LocationTableViewController ()
{
    UISearchBar *searchBar;
}
@end

@implementation LocationTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self navigationController].delegate = self;
    searchBar = [UISearchBar new];
    [searchBar setFrame: CGRectMake(8, 0, [[UIScreen mainScreen]bounds].size.width, 50)];
    [searchBar setTranslatesAutoresizingMaskIntoConstraints:YES];
    [self.locationTableView addSubview:searchBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)cancelButtonPressed:(id)sender {
    id<LocationTableViewControllerDelegate> strongDelegate = self.delegate;
    [strongDelegate LocationTableViewcontroller:self cancelButtonPressed:0.0f];
}
@end

Then instantiated it in my MapTabBarController.m

@interface MapTabBarController () <FPPopoverControllerDelegate, PrivacyPopoverDelegate, UITabBarControllerDelegate, LocationTableViewControllerDelegate>
@end

and I created the function for it:

- (void)LocationTableViewcontroller:(LocationTableViewController *)viewController cancelButtonPressed:(CGFloat)value {
    NSLog(@"We Clicked the Cancel Button!");
}

Problem that I'm having with this, is that it is never called - I have no NSLog in the console. Not sure what I've done wrong with this so need a fresh pair of eyes.

Upvotes: 0

Views: 919

Answers (1)

Mohamed DiaaEldin
Mohamed DiaaEldin

Reputation: 1061

What you should do When creating locationTableView

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES];    
locationTableView = [self.storyboard instantiateViewControllerWithIdentifier:@"LocationTableViewController"];
[self.view addSubview:locationTableView.view];

// Add this line

locationTableView.delegate = mapTabBarController

Upvotes: 1

Related Questions