[英]iOS: UISplitViewController show/hide master view button doesn't work
I am learning objective C. Now I am curios with UISplitViewController and show/hide master view button. 我正在学习目标C.现在我很喜欢UISplitViewController并显示/隐藏主视图按钮。 I have seen a lot of tutorial but I do something wrong and I don't know what is it.
我看过很多教程,但是我做错了什么,我不知道它是什么。 My button doesn't hide master view.
我的按钮不会隐藏主视图。 I absolutely broke my mind in two days.
两天后我绝对打破了我的想法。 Now I built this sample project to ask your advice.
现在我构建了这个示例项目来征求您的意见。 In my pattern I have: TabBarVC, SplitVC, TableViewVC and ViewVC.
在我的模式中,我有:TabBarVC,SplitVC,TableViewVC和ViewVC。 All controllers are my custom.
所有控制器都是我的定制。 MyTabBarViewController is coming first.
MyTabBarViewController首先出现。 I has the tab with UISplitViewController.
我有UISplitViewController的选项卡。
MySplitViewController.m MySplitViewController.m
#import "MySplitViewController.h"
#import "MyTabBarViewController.h"
#import "MyDetailViewController.h"
@interface MySplitViewController ()
@end
@implementation MySplitViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyTabBarViewController *masterVC = [self.storyboard instantiateViewControllerWithIdentifier:@"myTableVC"];
MyDetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"myDetailVC"];
NSArray *newViewControllersArray = [NSArray arrayWithObjects:masterVC, detailVC, nil];
self.viewControllers = newViewControllersArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
MyTableViewController.m MyTableViewController.m
#import "MyTableViewController.h"
@interface MyTableViewController ()
@end
@implementation MyTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArray = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataArray count];
}
//configure the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
return cell;
}
@end
MyDetailViewController.h MyDetailViewController.h
#import <UIKit/UIKit.h>
@interface MyDetailViewController : UIViewController <UISplitViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *showHideMasterVC;
@property (nonatomic, strong) UIPopoverController *popover;
@end
MyDetailViewController.m MyDetailViewController.m
#import "MyDetailViewController.h"
@interface MyDetailViewController ()
@end
@implementation MyDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)splitViewController:(UISplitViewController *)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)pc
{
//Grab a reference to the popover
self.popover = pc;
}
-(void)splitViewController:(UISplitViewController *)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
//Nil out the pointer to the popover.
_popover = nil;
}
@end
Here's my whole project with storyboard: MyProject 这是我的整个项目与故事板: MyProject
Sorry for a lot of code, but please help me or I get crazy. 很抱歉很多代码,但请帮助我或我发疯。 Thank you!
谢谢! PS By the way is it possible to have show/hide master view button outside NavigationController, on some view for example?
PS顺便说一下,在某些视图上可以在NavigationController外面显示/隐藏主视图按钮吗?
And now I got that - (void)splitViewController(UISplitViewController *)svc willHideViewController and - (void)splitViewController(UISplitViewController *)svc willShowViewController is deprecated in iOS8. 现在我得到了 - (void)splitViewController(UISplitViewController *)svc willHideViewController和 - (void)splitViewController(UISplitViewController *)svc willShowViewController在iOS8中已弃用。
So can somebody correct my project what I should do for solving my problem? 那么有人可以纠正我的项目我应该做些什么来解决我的问题?
I think the simplest way is to add self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
我认为最简单的方法是添加
self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
in your MyDetailViewController viewDidLoad
. 在您的MyDetailViewController
viewDidLoad
。 This will show the show/hide MasterDetailViewController button. 这将显示show / hide MasterDetailViewController按钮。
And your - (void)splitViewController(UISplitViewController *)svc willHideViewController
and - (void)splitViewController(UISplitViewController *)svc willShowViewController
is deprecated in iOS8. 你的
- (void)splitViewController(UISplitViewController *)svc willHideViewController
和- (void)splitViewController(UISplitViewController *)svc willShowViewController
在iOS8中已弃用。
I had a similar problem, and for me, I solved it by simply creating my project with Master-Detail Application template. 我遇到了类似的问题,对我而言,我只需使用Master-Detail Application模板创建项目即可解决问题。 It comes with show/button and
- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController
as a default. 它带有show / button和
- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController
作为默认值。 If you are using a TabBarViewController as a rootViewController then you might want to set that as the rootViewController in applicationDidFinishLaunchingWithOptions
. 如果您使用的是TabBarViewController作为RootViewController的,那么你可能要设置为在RootViewController的
applicationDidFinishLaunchingWithOptions
。 Hope this will help solve your problem. 希望这有助于解决您的问题。
Edit: 编辑:
PS By the way is it possible to have show/hide master view button outside NavigationController, on some view for example? PS顺便说一下,在某些视图上可以在NavigationController外面显示/隐藏主视图按钮吗?
First create a UIButton in your storyboard. 首先在故事板中创建一个UIButton。 Connect it to IBAction.
将其连接到IBAction。
- (IBAction)buttonAction:(id)sender {
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
[self.splitViewController.displayModeButtonItem action];
}
This should work. 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.