gpt4 book ai didi

uisplitviewcontroller - SplitViewController BUG?

转载 作者:行者123 更新时间:2023-12-01 05:21:31 28 4
gpt4 key购买 nike

我设置了一个非常简单的 iOS splitView Controller 项目,它有一个非常奇怪的错误。如果您以横向模式启动应用程序,请单击一个按钮以打开另一个 View ,旋转到纵向,然后返回到起始 View - 左侧的表格菜单应该不可见,但它是......奇怪的是你不能与它互动,这让我相信它不是真的存在,而是某种人工制品。

请注意,这只会在您第一次执行时发生,然后再也不会发生,直到您以横向模式重新启动应用程序。

主 View Controller :

#import "MasterViewController.h"

#import "DetailViewController.h"


@interface MasterViewController () {
NSMutableArray *_objects;
}

@property (nonatomic, strong)UITableView *menu;

@end

@implementation MasterViewController

- (void)awakeFromNib
{
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
[super awakeFromNib];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

}


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

- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem = object;
}

@end

细节 View Controller :
#import "DetailViewController.h"
#import "StandardViewViewController.h"


@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;

-(IBAction)buttonTapped:(id)sender;

- (void)configureView;
@end

@implementation DetailViewController

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;

// Update the view.
[self configureView];
}

if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}

- (void)configureView
{
// Update the user interface for the detail item.

if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self configureView];
}

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

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(@"Master", @"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}

-(IBAction)buttonTapped:(id)sender
{
StandardViewViewController *sv = [[StandardViewViewController alloc]initWithNibName:@"StandardViewViewController" bundle:nil];
[self.splitViewController presentViewController:sv animated:YES completion:nil];

}

@end

这是一个已知的错误?有解决办法吗?

最佳答案

虽然不漂亮,但解决方案建议here作品。
这是您实现解决方案的方式:

-(IBAction)buttonTapped:(id)sender
{
StandardViewViewController *sv = [[StandardViewViewController alloc] initWithNibName:nil bundle:nil];
sv.modalPresentationStyle = UIModalPresentationPageSheet; // <-- Add this
[self.splitViewController presentViewController:sv animated:YES completion:nil];
}

在 StandardViewController.m 中添加:
-(void)viewWillLayoutSubviews {
self.view.bounds = [StandardViewViewController screenBoundsForCurrentOrientation];
[super viewWillLayoutSubviews];
}

+(CGRect)screenBoundsForCurrentOrientation {
return [self screenBoundsForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

+(CGRect)screenBoundsForOrientation:(UIInterfaceOrientation)orientation {

UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds; //implicitly in Portrait orientation.

if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
CGRect temp = CGRectZero;
temp.size.width = fullScreenRect.size.height;
temp.size.height = fullScreenRect.size.width;
fullScreenRect = temp;
}

return fullScreenRect;
}

为了使它更漂亮,您可以将 StandardViewController 的代码放入 StandardViewController 可以扩展的新类中。

而且,是的,这看起来像是一个错误。

关于uisplitviewcontroller - SplitViewController BUG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16305336/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com