我有一个UIViewController,向其中添加了标签,按钮,tableView和searchView。我在系统中将此称为locationTableView
然后,我有另一个名为mapTabBarController
的viewController,当用户单击按钮时,我将其称为代码
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES];
locationTableView = [self.storyboard instantiateViewControllerWithIdentifier:@"LocationTableViewController"];
[self.view addSubview:locationTableView.view];
我禁用了
status bar
和navigation bar
,因为location view
是屏幕上的直接覆盖,但仍显示我们的mapTabBar
我目前正在使用“取消”按钮,它实际上只是隐藏
locationTableView
并将导航栏和状态栏重新显示。我最初试图使此功能正常运行- (IBAction)cancelButtonPressed:(id)sender {
[[UIApplication sharedApplication] setStatusBarHidden: NO];
[self.view removeFomSuperview];
}
这行得通,但是后来我不知道如何重新显示NavigationBar。因此,我想到了创建一个代表来做到这一点,因此我开始着手这一工作:
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
然后在我的MapTabBarController.m中实例化它
@interface MapTabBarController () <FPPopoverControllerDelegate, PrivacyPopoverDelegate, UITabBarControllerDelegate, LocationTableViewControllerDelegate>
@end
我为此创建了函数:
- (void)LocationTableViewcontroller:(LocationTableViewController *)viewController cancelButtonPressed:(CGFloat)value {
NSLog(@"We Clicked the Cancel Button!");
}
我遇到的问题是,它从未被调用-控制台中没有NSLog。不确定我做错了什么,所以需要换新的眼睛。
最佳答案
创建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
关于ios - objective-c UIViewController委托(delegate),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27159606/