2011-06-24 101 views
0

好的,請耐心等待:因爲這是Objective-C相關的問題,顯然有很多代碼和子類。所以這是我的問題。現在,我有一個iPad應用程序,它以編程方式創建一個按鈕和兩個彩色的UIViews。這些彩色的UIViews是由SubViewControllers控制的,整個事物都在一個由MainViewController控制的UIView中。 (即MainViewController = [UIButton的,SubViewController,SubViewController])現在ModalViewController for Single Subview

,這一切發生,因爲它應該和我結束了我所期望的(如下圖):

viewtools

然而,當我點擊按鈕,控制檯顯示「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:不工作模式的看法。應該看到翻轉。別。

+0

你在哪裏設置視圖爲SubViewController的視圖? – thomashw

回答

0

它看起來不像你在任何地方設置MainViewController的'view'屬性,只是'theView'。控制器視圖委託必須連接到它顯示的根視圖才能正常工作。您還需要在Sub View Controller impl上更正此問題。如果你想要框架類帶來的所有東西,你必須按照他們期望的方式來設置。

另外,您在其中一個子視圖控制器上調用presentModalViewController;改變這個來調用[self presentModalViewController:...],因爲MainViewController是'擁有'模態視圖的。

我認爲如果你修正了這些問題,你會發現-presentModalViewController會起作用。

+0

你建議的幫助,但它不完全是我想要的行爲:)我最終做的是創建一個視圖控制器與子視圖subView2,幷包含子視圖subView1子視圖。然後我使用UIViewAnimation將subView1轉換爲subView3。複雜,但有效。不管怎麼說,還是要謝謝你! –

+0

從您的MainViewController,您可以使用方法[-transitionFromView:toView:duration:options:completion:](http://developer.apple.com/library/ios/#DOCUMENTATION/)在任何子視圖和任何其他新子視圖之間進行動畫處理UIView上的UIKit/Reference/UIView_Class/UIView/UIView.html)。將現有子視圖作爲'from'參數傳遞,並將新視圖作爲'to'參數傳遞。 – RyanR