【问题标题】:Issues with UISplitViewController and modal UIViewControllersUISplitViewController 和模态 UIViewControllers 的问题
【发布时间】:2012-01-03 14:41:04
【问题描述】:

我的 iPhone/iPad 应用程序中有一个非常奇怪的 UI 故障。因为我想找到它的原因,所以我用尽可能少的代码创建了一个新项目。重要代码如下。

基本上,我有一个 UISplitViewController 包含两个 UIViewController 子类。当一个按钮被点击时,第一个按钮以模态方式呈现,并作为 UIModalPresentationFormSheet 一个名为 ModalUIViewController 子类。在那里,当一个按钮被点击时,另一个名为TextUIViewController 子类出现,这次是UIModalPresentationFullScreen。在Text 中有一个UITextView。当它被点击时,一切都很好,但是当 iPad 旋转时,我得到了这个:

白色部分是Text视图控制器,背景中的红色部分是ViewControllerTwo

有人知道为什么会这样吗?我能做些什么来解决它?


这里是项目:MediaFire


以下是相关源码:

// AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    ViewControllerOne *one = [[ViewControllerOne alloc] init];
    ViewControllerTwo *two = [[ViewControllerTwo alloc] init];

    UISplitViewController *split = [[UISplitViewController alloc] init];
    split.viewControllers = [NSArray arrayWithObjects:one, two, nil];

    self.window.rootViewController = split;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

// ViewControllerOne.m

#import "Modal.h"

@implementation ViewControllerOne

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(30, 30, 44, 44);

    [button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];
}

- (void)buttonTapped
{
    Modal *modalOne = [[Modal alloc] init];
    modalOne.modalPresentationStyle = UIModalPresentationFormSheet;

    [self presentModalViewController:modalOne animated:YES];
}

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

// Modal.m

#import "Text.h"

@implementation Modal

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonOne.frame = CGRectMake(30, 30, 44, 44);
    buttonOne.tag = 1;
    [buttonOne addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonTwo.frame = CGRectMake(30, 100, 44, 44);
    buttonTwo.tag = 2;
    [buttonTwo addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonOne];
    [self.view addSubview:buttonTwo];
}

- (void)buttonTapped:(UIButton *)button
{
    if (button.tag == 1)
    {
        Text *text = [[Text alloc] init];
        text.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentModalViewController:text animated:YES];
    }
    else
    {
        [self dismissModalViewControllerAnimated:YES];
    }
}

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

// Text.m

@implementation Text

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:textView];
}

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

【问题讨论】:

  • 您能否提供一个展示此行为的示例项目的下载链接?
  • @SebastianCelis 已上传,请参阅问题。
  • 您的存档无法构建。它引用您项目之外的文件。
  • @SebastianCelis 希望它现在可以工作。忘记“将项目复制到目标组的文件夹”...

标签: objective-c ipad uiviewcontroller


【解决方案1】:

您可能需要从 UISplitViewController 显示模态视图,而不是它的子视图...

UISplitViewController *splitViewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] splitViewController];

[splitViewController presentModalViewController:modal animated:YES];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多