2010-04-16 42 views
0

我在我的MainWindow.xib中有一個UIButtonUIButton點擊交換到另一個xib

當我點擊按鈕時,我想交換視圖。我怎麼做?

我也想轉移的意見(如顏色偏好和一個字符串)

任何示例代碼或鏈接到哪裏可以找到我的答案之間的一些數據將是非常有益的。

回答

1

alloc一個臨時視圖控制器,並且調用initWithNibName:。然後致電[self presentModalViewController:(the view controller you just made) animated:YES];(或NO)。要傳遞數據,請在您的其他視圖控制器上創建一個方法,將其添加到.h文件,然後在第一個視圖控制器的.m文件中,將其導入並創建一個類,並調用[theviewcontrollermadeearlier yourmethod:argument :argument etc.];例如:

MyFirstViewController.h:

#import <UIKit/UIKit.h> 
#import "MySecondViewController.h" 
... 
@class MySecondViewController 
... 

MyFirstViewController.m:

... 
MySecondViewController *tempVC = [[MySecondViewController alloc] initWithNibName:@"MySecondView"]; 
[self presentModalViewController:tempVC animated:YES]; 
[tempVC passDataWithString:@"a string" andColor:yellowcolor]; 

MySecondViewController.h:

@interface MySecondViewController : UIViewController { 
... 
} 
- (void)passDataWithString:(NSString *)passedString andColor:(UIColor *)passedColor; 

MySecondViewController.m:

... 
- (void)passDataWithString:(NSString *)passedString andColor:(UIColor *)passedColor { 
// Do something 
} 

編輯: 爲了使按鍵觸發此,在你的第一個視圖控制器的頭文件,在@interface部分添加IBOutlet IBAction *buttonPressed;,然後}@end之間添加- (IBAction)buttonPressed;
圍棋到Interface Builder中,並將IBAction連接到該按鈕。
然後,在你的第一個視圖控制器的主文件中加上:

- (IBAction)buttonPressed { 
    // The code to execute when pressed 
}