2011-03-30 33 views
0

是否可以在同一個.xib中顯示來自UIView或UIWindow的模態視圖?Objective-C模態視圖

這裏是我的代碼:

@interface MyViewController : UIViewController <UIWebViewDelegate> { 
IBOutlet UIVIew *myView 
} 

@property (nonatomic, retain) UIView *myView; 

- (void)somefunction; 

我的功能:

- (void)somefunction { 
    //here i need to make my view1 gets presented by a modal view style. 
} 
+0

爲什麼不將視圖移動到它自己的NIB文件?它會減少應用程序的內存佔用量,並讓'UIViewController'以它們設計的方式工作。 – kubi 2011-03-30 19:28:42

回答

0

您需要添加IBOutlets您的觀點。在您的MyViewController.h文件中:

... 
IBOutlet UIView *firstView; 
IBOutlet UIView *secondView; 
... 

然後使用Interface Builder(或Xcode 4)連接插座。之後,在您的實施文件中,您可以手動隱藏或顯示所需的視圖。

PresenterViewController.m:

- (void)ShowSecondView{ 

    //Initialize the view controller 
    MyViewController *modalView = [[MyViewController alloc] initWithNibNamed:@"MyViewController" bundle:nil]; 

    //Hide or show whatever view(s) you want 
    [modalView.firstView setHidden:YES]; 
    [modalView.secondView setHidden:NO]; 

    //Present the view controller modally 
    [self presentModalViewController:modalView animated:YES]; 

    //Don't forget memory management! 
    [modalView release]; 
} 

您可能需要調整該代碼一點點,但這是一般的想法。

+0

好的。你沒有解釋我的問題。我有2個視圖,加載的主視圖和另一個視圖,我們稱之爲「視圖2」。兩者都在MainViewController.xib中。我想知道如何展現View2像Modal。 – 2011-03-30 19:15:46

+0

啊,好的。您需要定義IBOutlets並使用它們來隱藏/顯示視圖。例如:'IBOutlet UIView * view1;'等...請編輯該問題,以便我可以編輯我的答案以匹配。 – Moshe 2011-03-30 19:17:57

+0

我編輯過。這是否更好? – Moshe 2011-03-30 19:23:24