小時前我發佈了一個關於在iPhone中組織縱向和橫向模式的問題,現在我想我知道如何使用willRotateToInterfaceOrientation:duration。iPhone在navigationViewController模式下旋轉視圖
第一個屏幕是「地圖視圖」,其中一個按鈕導致「設置視圖」。地圖視圖不支持旋轉,但對於設置視圖,我爲縱向和橫向製作了單獨的視圖,並在旋轉時相應地進行交換。
,,
正如你可以看到當設置按鈕按下SettingView被視圖棧照常上加入。所以基本上我使用三個視圖控制器;設置,設置風景和SettingPortrait。
我仍然發現在iPhone中使用navigationViewController時旋轉視圖的問題。 分段控制不起作用。它崩潰沒有錯誤信息。它以前沒有旋轉就能正常工作.-當我不使用多視圖旋轉時。
rotateViewController.m
這是根視圖控制器。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(IBAction) buttonPressed{
Setting *settingViewController = [[Setting alloc] initWithNibName:@"Setting" bundle:[NSBundle mainBundle]];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController: settingViewController];
[self.navigationController presentModalViewController:navController1 animated:YES];
[settingViewController release];
[navController1 release];
}
Setting.m
此視圖控制器不只是交換意見時,旋轉並顯示縱向和橫向之間相應的視圖。
在Setting.m中,我換成如下的視圖;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
NSLog(@"to Right");
SettingLandscape *setting_landscape = [[SettingLandscape alloc] initWithNibName:@"SettingLandscape" bundle:[NSBundle mainBundle]];
self.view = setting_landscape.view;
[setting_landscape release];
}
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
NSLog(@"to Left");
SettingLandscape *setting_landscape = [[SettingLandscape alloc] initWithNibName:@"SettingLandscape" bundle:[NSBundle mainBundle]];
self.view = setting_landscape.view;
[setting_landscape release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
NSLog(@"to Portrait");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"to PortraitUpsideDown");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
}
在viewWillAppear中,設置還具有視圖控制器;
self.title = @"Shell ";
self.navigationController.navigationBarHidden = NO;
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(Done)] autorelease];
和完成是
- (void) Done{
[self dismissModalViewControllerAnimated:YES];
}
SettingLandscape.m
此視圖堆疊時的視圖上旋轉。這個視圖控制器有它的導航欄。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = @"Setting Landscape";
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
在viewDidLoad中;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"landscape:viewDidLoad");
//self.title = @"SettingLandscape";//not working!!
//self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done1" style:UIBarButtonItemStylePlain target:self action:@selector(Done)] autorelease];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
stringflag4MapType = [[NSString alloc] initWithString:@"blah"];
stringflag4MapType = [defaults stringForKey:@"flag4MapType"];
if (![stringflag4MapType isEqualToString:@"Hybrid"] && ![stringflag4MapType isEqualToString:@"Standard"] && ![stringflag4MapType isEqualToString:@"Satellite"]) {
segmentedControl4MapType.selectedSegmentIndex = 0;
}else if ([self.stringflag4MapType isEqualToString:@"Standard"]) {
segmentedControl4MapType.selectedSegmentIndex = 0;
}else if ([self.stringflag4MapType isEqualToString:@"Satellite"]) {
segmentedControl4MapType.selectedSegmentIndex = 1;
}else if ([self.stringflag4MapType isEqualToString:@"Hybrid"]) {
segmentedControl4MapType.selectedSegmentIndex = 2;
}
以下調用不會被調用。奇怪。無論如何,旋轉工作無關緊要。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
NSLog(@"to Portrait");// does not print out.
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"to PortraitUpsideDown");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
}
現在OK,你可以從那些抓拍看到有兩個導航欄各有其欄按鈕,完成和項目。完成按鈕來自Setting和SettingPortrait或SettingLandscape中的Item按鈕。所有按鈕的選擇器都是相同的,並返回到地圖視圖。按鈕完成工作正常,但按鈕項目崩潰。旋轉後我需要一個導航欄上的按鈕,就像後退按鈕一樣。我想我有一次'self.view = settingportrait.view;'問題就開始了。
我之所以需要項目按鈕的工作是分段的控制開始崩潰,一旦我添加代碼來支持旋轉。如果我找到理由如何製作項目按鈕 - 即在旋轉視圖內 - 工作,我想我也可以使分段控制工作。
您可以在https://github.com/downloads/bicbac/rotation-test/rotate-1.zip
謝謝,我之所以用兩個獨立的觀點是這樣的,當我旋轉視圖我的UI佈局劑量不適合屏幕。如果不使用獨立的視圖旋轉,你如何管理用戶界面呢?例如屏幕截圖簡化ü是。沒有錯誤消息。 「所以,當你點擊項目或分段控制,它會調用委託,這可能是設置爲setting_landscape |肖像其中已經釋放。」 < - 這可能是一個線索.... – bicbac 2011-03-11 04:32:29
如果你想保持獨立的視圖旋轉,然後構造兩個視圖中設置視圖控制器和交換他們根據旋轉。這樣,視圖的代表將設置視圖控制器。 順便說一句,你是什麼意思的觀點不適合?你想要一個完全不同的菜單模式?或者,如果你只需要左右移動的東西,使之貼合,只需設置幀/時中心「 - (空)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation時間:(NSTimeInterval)持續時間 」功能。這也增加了所有動畫元素的獎勵。 – 2011-03-11 04:49:53
其實我打算讓應用爲iPad據我所知,每個視圖必須旋轉以獲得接受。是的,我只需要移動它以適應它。我想我應該看看這個電話。非常感謝。 – bicbac 2011-03-11 06:57:31