简体   繁体   English

目标C UIViewController委托

[英]Objective C UIViewController Delegate

I have a UIViewController to which I've added a label, a button, a tableView and a searchView. 我有一个UIViewController,向其中添加了标签,按钮,tableView和searchView。 I call this locationTableView in my system 我在系统中称这个locationTableView

I then have another viewController called mapTabBarController which when the user clicks on a button I call this code 然后,我还有另一个名为mapTabBarController ,当用户单击按钮时,我将此代码称为

[[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 我禁用status barnavigation bar因为location view直接覆盖在屏幕上,但仍显示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. 我目前正在使用“取消”按钮,它实际上只是隐藏了locationTableView并将导航栏和状态栏重新显示。 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. 这行得通,但是后来我不知道如何让NavigationBar重新显示。 So I thought of creating a delegate to do it, so I began working on that: 因此,我想到了要创建一个委托来做到这一点,因此我开始着手这一工作:

LocationTableViewController.h 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 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 然后在我的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. 我遇到的问题是,它从未被调用-控制台中没有NSLog。 Not sure what I've done wrong with this so need a fresh pair of eyes. 不确定我做错了什么,所以需要换新的眼睛。

What you should do When creating locationTableView 创建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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM