Iphone 帶NavigationBar的ModalView
Posted on 2010-10-06 18:16 Neil's NoteBook 閱讀(988) 評論(0) 編輯 收藏 所屬分類: Iphone Development悲劇的thinkpad。。。所以不能截圖。。。稀爛!
1. 創建一個viewcontroller,比如SettingViewController,同時創建實現文件和頭文件,不多說
2. 創建該viewcontroller對應的view文件,比如SettingView.xib,沒什么好說的
3. 雙擊剛才創建的xib文件,指定class為第一步創建的viewcontroller,在interface builder中將view和file owner連接起來
4. 在創建的SettingViewController.h文件中定義一個bool類型的變量,該變量用來指示modal view是否已彈出,代碼如下:
@interface SettingViewController : UIViewController {
BOOL isPushedView;
}
@property (nonatomic, readwrite) BOOL isPushedView;
5. 在SettingViewController.m文件中添加具體實現代碼,如下:
@implementation SettingViewController @synthesize isPushedView; - (void)viewDidLoad { [super viewDidLoad]; if(isPushedView == NO) { self.navigationItem.title = @"設置"; self.navigationController.navigationBar.barStyle = UIBarStyleBlack; self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(cancel_Clicked:)] autorelease]; } } -(void) cancel_Clicked:(id)sender { [self.navigationController dismissModalViewControllerAnimated:YES]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); }
@class SettingViewController;
@interface RootViewController : UITableViewController {
SettingViewController *settingViewController;
UINavigationController *settingNavController;
}
7. 在RootViewController.m文件中添加實現代碼,如下:
- (void) settingClicked {
if(settingViewController == nil) {
settingViewController = [[SettingViewController alloc] initWithNibName:@"SettingView" bundle:[NSBundle mainBundle]];
settingViewController.isPushedView = NO;
}
if(settingNavController == nil) {
settingNavController = [[UINavigationController alloc] initWithRootViewController:settingViewController];
[self.navigationController presentModalViewController:settingNavController animated:YES];
}
}
8. DONE!!!