gpt4 book ai didi

iphone - SplitViewController 未显示

转载 作者:行者123 更新时间:2023-12-03 20:15:38 26 4
gpt4 key购买 nike

我现在拼命地尝试了 2 个小时来找到我的代码中的一个错误,该错误导致没有显示我已经实现的 SplitViewController 。奇怪的是,没有显示编译错误,并且所有链接和依赖项似乎都是正确的。

首次加载时屏幕上没有显示任何内容。但是当我旋转设备时,我看到了正确的 View 。但是当启动时,即使所有 View 都已连接,也根本没有 View 。

我刚刚开始工作......我通过调试器发现,第一个 View Controller 在启动时未加载。因此,分割 View 的 View Controller 数组没有填充初始 View Controller ......我该如何解决这个问题?

我检查的第一部分是我的 AppDelegate:

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize splitViewController = _splitViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[[self window] addSubview:self.splitViewController.view];
[self.window makeKeyAndVisible];

return YES;
}



@end

第一个 View Controller

#import "FirstViewController.h"

@implementation FirstViewController
@synthesize toolbar;


#pragma mark -
#pragma mark View Lifecycle

- (void)viewDidUnload
{
[super viewDidUnload];
self.toolbar = nil;
}


#pragma mark -
#pragma mark Managing the Popover

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem
{
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray insertObject:barButtonItem atIndex:0];
[toolbar setItems:itemsArray animated:NO];
}

- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem
{
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray removeObject:barButtonItem];
[toolbar setItems:itemsArray animated:NO];
}

#pragma mark -
#pragma mark Rotation support

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}


@end

我检查的第二部分是我的 RootViewController。问题是,当您单击 tableView 条目时,将加载并显示新的 View Controller 。因此,我不在应用程序委托(delegate)中实例化 VC,而是在“选择行” TableView 委托(delegate)方法中实例化 VC。

这是 Root View Controller :

#import "RootViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation RootViewController
@synthesize popoverController, splitViewController, rootPopoverButtonItem;


#pragma mark -
#pragma mark View Lifecycle

- (void)viewDidLoad
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[super viewDidLoad];
self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight * 2.0);
}

- (void)viewDidUnload
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[super viewDidUnload];
self.splitViewController = nil;
self.rootPopoverButtonItem = nil;
}


#pragma mark -
#pragma mark Rotation support

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
return YES;
}


#pragma mark -
#pragma mark SplitViewController delegates

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
barButtonItem.title = @"PolyCube Prototypes";

self.popoverController = pc;
self.rootPopoverButtonItem = barButtonItem;

UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
[detailViewController showRootPopoverButtonItem:rootPopoverButtonItem];
}

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
self.popoverController = nil;
self.rootPopoverButtonItem = nil;

UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
[detailViewController invalidateRootPopoverButtonItem:rootPopoverButtonItem];
}



#pragma mark -
#pragma mark Table View Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
static NSString *CellIdentifier = @"RootViewControllerIdentifier";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if (indexPath.row == 0) {
cell.textLabel.text = @"PolyCube 1";
}
else {
cell.textLabel.text = @"PolyCube 2";
}

return cell;
}


#pragma mark -
#pragma mark - Table View Selection

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
NSUInteger row = indexPath.row;

UIViewController <SubstitutableDetailViewController> *detailViewController = nil;

if (row == 0) {
FirstViewController *newDetailViewController = [[FirstViewController alloc] initWithNibName:@"FirstDetailView" bundle:nil];
detailViewController = newDetailViewController;
}


if (row == 1) {
SecondViewController *newDetailViewController = [[SecondViewController alloc] initWithNibName:@"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}


NSArray *vcs = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = vcs;

if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}

if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
}

@end

我真的很感谢您的帮助...我不知道该去哪里寻找了。我什至检查了所有 IB 连接...我认为没有任何问题。但仍然只是白屏。

非常感谢塞巴斯蒂安

最佳答案

这种行为是预期的。如果您不设置 UISplitViewController 的委托(delegate),则主视图在纵向模式下将不可见。您的主视图永远不会加载,您看到的白色屏幕是您的详细 View 。

因此,您需要实现自己的 UISplitViewController(创建一个新的 UIViewController 并将其更改为 UISplitViewControlle。不要忘记在 Storyboard 中将其分配给您的分割 View )

同样在接口(interface)中,使其实现并在 viewDidLoad 方法中将委托(delegate)设置为 self。

该协议(protocol)将告诉把主表放在哪里。你想要像分割模式一样,分割窗口吗?或者您可能想要在某个地方有一个按钮(通常在选项卡栏中),当您按下它时,主窗口会弹出?

例如,实现这个家伙让主视图始终可见:

接口(interface):

#import <UIKit/UIKit.h>

@interface SplitViewController : UISplitViewController <UISplitViewControllerDelegate>

@end

实现

#import "SplitViewController.h"

@interface SplitViewController ()

@end

@implementation SplitViewController


- (void)viewDidLoad
{
[super viewDidLoad];
[self setDelegate:self];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation{
return NO;
}


@end

如果您不想实现自己的 UISplitViewController 只是为了处理委托(delegate),您还可以让任何主 Controller 或细节 Controller 来实现它并在 Storyboard 中分配它(尝试按住 ctrl 并从 Split View拖动到您的任何 Controller 并将其设置为委托(delegate),不要忘记首先实现协议(protocol))

关于iphone - SplitViewController 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8694779/

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