【问题标题】:ModalViewController for Single Subview用于单个子视图的 ModalViewController
【发布时间】:2011-06-24 13:49:57
【问题描述】:

好的,请耐心等待:由于这是一个与 Objective-C 相关的问题,显然有很多代码和子类化。所以这是我的问题。现在,我有一个 iPad 应用程序,它以编程方式创建一个按钮和两个彩色 UIView。这些彩色的 UIView 是由 SubViewControllers 控制的,整个东西都在一个由 MainViewController 控制的 UIView 中。 (即 MainViewController = [UIButton, SubViewController, SubViewController])

现在,所有这一切都发生了,我最终得到了我所期望的结果(如下):

但是,当我单击按钮时,控制台显示“flipSubView1”,什么也没有发生。没有显示模态视图,也没有发生错误。根本不值一提。我期望的是 subView1 或整个视图将水平翻转并显示 subView3。是否有一些我遗漏的代码会导致这种情况发生/是否有一些我忽略的错误?

viewtoolsAppDelegate.m

@implementation viewtoolsAppDelegate

@synthesize window = _window;
@synthesize mvc;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    mvc = [[MainViewController alloc] initWithFrame:self.window.frame];
    [self.window addSubview:mvc.theView];
    [self.window makeKeyAndVisible];

    return YES;
}

MainViewController.m

@implementation MainViewController

@synthesize theView;
@synthesize subView1, subView2, subView3;

- (id)initWithFrame:(CGRect) frame
{
    theView = [[UIView alloc] initWithFrame:frame];
    CGRect sV1Rect = CGRectMake(frame.origin.x+44, frame.origin.y, frame.size.width-44, frame.size.height/2);
    CGRect sV2Rect = CGRectMake(frame.origin.x+44, frame.origin.y+frame.size.height/2, frame.size.width-44, frame.size.height/2);
    subView1 = [[SubViewController alloc] initWithFrame:sV1Rect andColor:[UIColor blueColor]];
    subView2 = [[SubViewController alloc] initWithFrame:sV2Rect andColor:[UIColor greenColor]];
    subView3 = [[SubViewController alloc] initWithFrame:sV1Rect andColor:[UIColor redColor]];
    [theView addSubview:subView1.theView];
    [theView addSubview:subView2.theView];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton addTarget:self action:@selector(flipSubView1:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
    [aButton setFrame:CGRectMake(0, 0, 44, frame.size.height)];
    [theView addSubview:aButton];

    return self;
}

- (void)flipSubView1:(id) sender
{
    NSLog(@"flipSubView1");
    [subView3 setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [subView1 presentModalViewController:subView3 animated:YES];
}

SubViewController.m

@implementation SubViewController

@synthesize theView;

- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)color
{
    theView = [[UIView alloc] initWithFrame:frame];
    theView.backgroundColor = color;
    return self;
}

TLDR:模态视图不起作用。应该看到翻转。不要。

【问题讨论】:

  • 你在哪里设置 theView 为 SubViewController 的视图?

标签: objective-c ios view controller modal-dialog


【解决方案1】:

您似乎没有在任何地方设置 MainViewController 的“view”属性,只是“theView”。控制器视图委托必须连接到它显示的根视图才能正常工作。您还需要在 Sub View Controller impl 上进行更正。如果您想要框架类带来的所有管道,您必须按照他们期望的方式进行设置。

此外,您正在其中一个子视图控制器上调用 presentModalViewController;将其更改为调用 [self presentModalViewController:...],因为 MainViewController 将“拥有”模态视图。

我认为如果你解决了这些问题,你会发现 -presentModalViewController 会起作用。

【讨论】:

  • 您的建议有所帮助,但这并不是我想要的行为:) 我最终做的是创建一个视图控制器,其中包含 subView2 的子视图,以及包含 subView1 的子视图的子视图。然后我使用 UIViewAnimation 将 subView1 翻转为 subView3。复杂,但有效。还是谢谢!
  • 从您的 MainViewController 中,您可以使用 UIView 上的 -transitionFromView:toView:duration:options:completion: 方法在任何子视图和任何其他新子视图之间进行动画处理。将现有子视图作为“from”参数传递,将新视图作为“to”参数传递。
猜你喜欢
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多