更多视图和控制器的方向问题 (iPad)
我正在使用 Orientation 为 iPad 编写一个应用程序。 App-Delegate.h 有一个窗口、一个 UIViewController、一个 UINavigationController 和一个 UITabbarController:
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LoginRVC *loginRVC;
@property (nonatomic, retain) IBOutlet ChooseCameraRVC *chooseCameraRVC;
@property (nonatomic, retain) IBOutlet UITabBarController *hauptRVC;
每个控制器都使用“shouldAutorotateToInterfaceOrientation”方法来自动旋转。
我使用以下方法更改视图:
[UIView beginAnimations:nil context:NULL];
然后
[loginRVC.view removeFromSuperview];
[_window addSubview:chooseCameraRVC.view];
,反过来,ofc。
所以我的问题是,当我处于第二个视图(选择CameraRVC)并切换方向,然后返回到我的第一个视图时,它没有旋转。它确实会自动旋转,但在动画完成后。
我尝试了很多方法,例如调用所有视图的“shouldAutorotateToInterfaceOrientation”方法,而不是从窗口中删除视图......但到目前为止还没有成功。
这可能是模拟器的“功能”吗? (我希望不是)。
请帮助我。 鲨鱼
好的。我准备了我的源代码,在这里展示。
注意:我没有复制那些只有 [super ...] 的方法或者被完全注释掉的方法。
首先是AppDelegate.h:
#import <UIKit/UIKit.h>
#import "ChooseCameraRVC.h"
#import "LoginRVC.h"
@interface NetCoWatchAppDelegate : NSObject <UIApplicationDelegate>
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LoginRVC *loginRVC;
@property (nonatomic, retain) IBOutlet ChooseCameraRVC *chooseCameraRVC;
-(void)changeView:(id)sender:(BOOL)direction;
@end
AppDelegate.m:
#import "NetCoWatchAppDelegate.h"
#import "LoginRVC.h"
#import "ChooseCameraRVC.h"
#import "ChooseCameraVC.h"
@implementation NetCoWatchAppDelegate
@synthesize window = _window;
@synthesize loginRVC, chooseCameraRVC;
-(void)changeView:(id)sender:(BOOL)direction{
//configure animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
if(sender == loginRVC){ //sender is LoginView
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_window cache:YES];
[loginRVC.view removeFromSuperview];
[_window addSubview:chooseCameraRVC.view];
}else if(sender == chooseCameraRVC){
[chooseCameraRVC.view removeFromSuperview];
if(!direction){ //FlipFromRight = YES, ...left = NO
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[_window addSubview:loginRVC.view];
}
}else if([sender class] == [ChooseCameraVC class]){
[chooseCameraRVC.view removeFromSuperview];
if(!direction){ //Camera gewählt //FlipFromRight = YES, ...left = NO
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[_window addSubview:loginRVC.view];
}
}else { //default solution
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Bad Value" message:[[sender class] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[av release];
}
[UIView commitAnimations]; //start animation
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[loginRVC release];
[chooseCameraRVC release];
[super dealloc];
}
@end
LoginRVC.h:
#import <UIKit/UIKit.h>
@interface LoginRVC : UIViewController <UITextFieldDelegate>{
NSMutableArray *usernameArray;
NSMutableArray *passwordArray;
}
@property (nonatomic, retain) IBOutlet UITextField *usernameTF;
@property (nonatomic, retain) IBOutlet UITextField *passwordTF;
@property (nonatomic, retain) IBOutlet UIButton *loginBn;
@property (nonatomic, retain) IBOutlet UISwitch *saveUsernameSwitch;
-(IBAction)tryLogin:(id)sender;
-(IBAction)closeKeyboard:(id)sender;
@end
LoginRVC.m:
#import "LoginRVC.h"
#import "NetCoWatchAppDelegate.h"
@implementation LoginRVC
@synthesize usernameTF, passwordTF, loginBn, saveUsernameSwitch;
-(IBAction)tryLogin:(id)sender{
//login successful if the textfields are euqal with an existing account
#warning Access the data base and search for the account.
bool accountFound = NO;
for (int i=0; i<usernameArray.count; i++) {
if([[usernameArray objectAtIndex:i] isEqualToString:usernameTF.text]
&& [[passwordArray objectAtIndex:i] isEqualToString:passwordTF.text]){
accountFound = YES;
break;
}
}
if(accountFound)
{ //login successful - now change the values and then the view
if(![saveUsernameSwitch isOn])
usernameTF.text = @"";
passwordTF.text = @"";
NetCoWatchAppDelegate *main = (NetCoWatchAppDelegate*)[[UIApplication sharedApplication] delegate];
[main changeView:self:YES];
}else{ //login failt - show a popup window for the user
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Login fehlgeschlagen" message:@"Username oder Passwort falsch!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[av release];
}
}
-(IBAction)closeKeyboard:(id)sender{
if([passwordTF isFirstResponder])
[passwordTF resignFirstResponder];
else
[usernameTF resignFirstResponder];
}
// this helps dismiss the keyboard then the "done" button is clicked
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == usernameTF){ //move to password textfield
[textField resignFirstResponder];
[passwordTF becomeFirstResponder];
}else if(textField == passwordTF){ //textField == passwordTF -> try to login
[textField resignFirstResponder];
[self tryLogin:self];
}
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
#warning Define right keyboard type.
usernameArray = [[NSMutableArray alloc] initWithObjects:@"dkoehn", @"bmazanek", @"sbehne", @"mballhausen", @"efiedler", @"bbraasch", @"azuber", @"tstolt", nil];
passwordArray = [[NSMutableArray alloc] initWithObjects:@"test1",@"test2",@"test3",@"test4",@"test5",@"test6",@"test7",@"test8", nil];
// usernameTF.keyboardType = UIKeyboardTypeEmailAddress;
[usernameTF becomeFirstResponder]; //get first focus when the app stars
//set return key on the keyboard and the delegate for an action
usernameTF.returnKeyType = UIReturnKeyNext; // type of the return key
passwordTF.returnKeyType = UIReturnKeyGo;
//set delegate to connect with a method "-(BOOL)textFieldShouldReturn:(UITextField *)textField"
usernameTF.delegate = self;
passwordTF.delegate = self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
ChooseCameraRVC.h:
#import <UIKit/UIKit.h>
@interface ChooseCameraRVC : UINavigationController <UINavigationControllerDelegate>
@property (nonatomic, retain) IBOutlet UIBarButtonItem *zurueckBN;
-(IBAction)exitToLoginView:(id)sender;
@end
ChooseCameraRVC.m:
#import "ChooseCameraRVC.h"
#import "NetCoWatchAppDelegate.h"
#import "ChooseCameraCell.h"
@implementation ChooseCameraRVC
@synthesize zurueckBN;
-(IBAction)exitToLoginView:(id)sender{
#warning Eventually logout the User.
//change the view
[((NetCoWatchAppDelegate*)[[UIApplication sharedApplication] delegate]) changeView:self:NO];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
ChooseCameraVC.h:
#import <UIKit/UIKit.h>
@interface ChooseCameraVC : UITableViewController <UITableViewDelegate>
@end
和ChooseCameraVC.m:
#import "ChooseCameraVC.h"
#import "ChooseCameraCell.h"
#import "NetCoWatchAppDelegate.h"
@implementation ChooseCameraVC
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Customize the number of sections if grouped.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
#warning Get count of cameras out of the data base.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = @"Camera";
return cell;
}
@end
我希望你能找到问题所在。
问候。 $h@rky
Im writing an App for iPad using Orientation.
The App-Delegate.h has a window, an UIViewController, an UINavigationController and an UITabbarController:
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LoginRVC *loginRVC;
@property (nonatomic, retain) IBOutlet ChooseCameraRVC *chooseCameraRVC;
@property (nonatomic, retain) IBOutlet UITabBarController *hauptRVC;
Every Controller uses the "shouldAutorotateToInterfaceOrientation"-method to autorotate itself.
i change the views using:
[UIView beginAnimations:nil context:NULL];
and then
[loginRVC.view removeFromSuperview];
[_window addSubview:chooseCameraRVC.view];
and the other way around too, ofc.
So my problem is, when i am in the second view (chooseCameraRVC) and switch the orientation, then go back to my first view, its not rotated. It do autorotate but after the animation is completed.
I tried many things like calling "shouldAutorotateToInterfaceOrientation"-method of all views, not removing the views from window ... but no success til now.
Is this maybe a "feature" of the simulator? (i hope not).
Pls help me.
Sharky
Ok. I prepared my source code to be presented here.
Note: I didn't copy the methods which only has [super ...] within or are completely commented out.
At first the AppDelegate.h:
#import <UIKit/UIKit.h>
#import "ChooseCameraRVC.h"
#import "LoginRVC.h"
@interface NetCoWatchAppDelegate : NSObject <UIApplicationDelegate>
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LoginRVC *loginRVC;
@property (nonatomic, retain) IBOutlet ChooseCameraRVC *chooseCameraRVC;
-(void)changeView:(id)sender:(BOOL)direction;
@end
AppDelegate.m:
#import "NetCoWatchAppDelegate.h"
#import "LoginRVC.h"
#import "ChooseCameraRVC.h"
#import "ChooseCameraVC.h"
@implementation NetCoWatchAppDelegate
@synthesize window = _window;
@synthesize loginRVC, chooseCameraRVC;
-(void)changeView:(id)sender:(BOOL)direction{
//configure animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
if(sender == loginRVC){ //sender is LoginView
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_window cache:YES];
[loginRVC.view removeFromSuperview];
[_window addSubview:chooseCameraRVC.view];
}else if(sender == chooseCameraRVC){
[chooseCameraRVC.view removeFromSuperview];
if(!direction){ //FlipFromRight = YES, ...left = NO
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[_window addSubview:loginRVC.view];
}
}else if([sender class] == [ChooseCameraVC class]){
[chooseCameraRVC.view removeFromSuperview];
if(!direction){ //Camera gewählt //FlipFromRight = YES, ...left = NO
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[_window addSubview:loginRVC.view];
}
}else { //default solution
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Bad Value" message:[[sender class] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[av release];
}
[UIView commitAnimations]; //start animation
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[loginRVC release];
[chooseCameraRVC release];
[super dealloc];
}
@end
The LoginRVC.h:
#import <UIKit/UIKit.h>
@interface LoginRVC : UIViewController <UITextFieldDelegate>{
NSMutableArray *usernameArray;
NSMutableArray *passwordArray;
}
@property (nonatomic, retain) IBOutlet UITextField *usernameTF;
@property (nonatomic, retain) IBOutlet UITextField *passwordTF;
@property (nonatomic, retain) IBOutlet UIButton *loginBn;
@property (nonatomic, retain) IBOutlet UISwitch *saveUsernameSwitch;
-(IBAction)tryLogin:(id)sender;
-(IBAction)closeKeyboard:(id)sender;
@end
The LoginRVC.m:
#import "LoginRVC.h"
#import "NetCoWatchAppDelegate.h"
@implementation LoginRVC
@synthesize usernameTF, passwordTF, loginBn, saveUsernameSwitch;
-(IBAction)tryLogin:(id)sender{
//login successful if the textfields are euqal with an existing account
#warning Access the data base and search for the account.
bool accountFound = NO;
for (int i=0; i<usernameArray.count; i++) {
if([[usernameArray objectAtIndex:i] isEqualToString:usernameTF.text]
&& [[passwordArray objectAtIndex:i] isEqualToString:passwordTF.text]){
accountFound = YES;
break;
}
}
if(accountFound)
{ //login successful - now change the values and then the view
if(![saveUsernameSwitch isOn])
usernameTF.text = @"";
passwordTF.text = @"";
NetCoWatchAppDelegate *main = (NetCoWatchAppDelegate*)[[UIApplication sharedApplication] delegate];
[main changeView:self:YES];
}else{ //login failt - show a popup window for the user
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Login fehlgeschlagen" message:@"Username oder Passwort falsch!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[av release];
}
}
-(IBAction)closeKeyboard:(id)sender{
if([passwordTF isFirstResponder])
[passwordTF resignFirstResponder];
else
[usernameTF resignFirstResponder];
}
// this helps dismiss the keyboard then the "done" button is clicked
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == usernameTF){ //move to password textfield
[textField resignFirstResponder];
[passwordTF becomeFirstResponder];
}else if(textField == passwordTF){ //textField == passwordTF -> try to login
[textField resignFirstResponder];
[self tryLogin:self];
}
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
#warning Define right keyboard type.
usernameArray = [[NSMutableArray alloc] initWithObjects:@"dkoehn", @"bmazanek", @"sbehne", @"mballhausen", @"efiedler", @"bbraasch", @"azuber", @"tstolt", nil];
passwordArray = [[NSMutableArray alloc] initWithObjects:@"test1",@"test2",@"test3",@"test4",@"test5",@"test6",@"test7",@"test8", nil];
// usernameTF.keyboardType = UIKeyboardTypeEmailAddress;
[usernameTF becomeFirstResponder]; //get first focus when the app stars
//set return key on the keyboard and the delegate for an action
usernameTF.returnKeyType = UIReturnKeyNext; // type of the return key
passwordTF.returnKeyType = UIReturnKeyGo;
//set delegate to connect with a method "-(BOOL)textFieldShouldReturn:(UITextField *)textField"
usernameTF.delegate = self;
passwordTF.delegate = self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
The ChooseCameraRVC.h:
#import <UIKit/UIKit.h>
@interface ChooseCameraRVC : UINavigationController <UINavigationControllerDelegate>
@property (nonatomic, retain) IBOutlet UIBarButtonItem *zurueckBN;
-(IBAction)exitToLoginView:(id)sender;
@end
The ChooseCameraRVC.m:
#import "ChooseCameraRVC.h"
#import "NetCoWatchAppDelegate.h"
#import "ChooseCameraCell.h"
@implementation ChooseCameraRVC
@synthesize zurueckBN;
-(IBAction)exitToLoginView:(id)sender{
#warning Eventually logout the User.
//change the view
[((NetCoWatchAppDelegate*)[[UIApplication sharedApplication] delegate]) changeView:self:NO];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
ChooseCameraVC.h:
#import <UIKit/UIKit.h>
@interface ChooseCameraVC : UITableViewController <UITableViewDelegate>
@end
and the ChooseCameraVC.m:
#import "ChooseCameraVC.h"
#import "ChooseCameraCell.h"
#import "NetCoWatchAppDelegate.h"
@implementation ChooseCameraVC
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Customize the number of sections if grouped.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
#warning Get count of cameras out of the data base.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = @"Camera";
return cell;
}
@end
I hope u can find the problem.
Greetings. $h@rky
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在我发现了我的错误。正如你所看到的,我将视图作为应用程序委托中的变量。因此,如果第二个视图改变了方向,其他视图对此一无所知。如果视图现在更改“新”视图,则会在动画之后识别方向更改,因此在动画运行时,“新”视图的方向错误。
因此,如果您想切换视图,只需创建一个新视图,因为它会以正确的方向初始化。
亲切的问候
$h@rky
now i found my mistake. as u can see i have the views as variables in the app delegate. so if the second view changes the orientation, the other ones didn't know a thing about it. if the view now changes the "new" one recognizes the orientation change AFTER the animation, so while the animation is running, the "new" view has the wrong orientation.
So if u want to switch a view, just create a new one because it gets initialized with the right orientation.
kind regards
$h@rky
为了支持所有方向,您的视图控制器应该实现shouldAutorotateToInterfaceOrientation,如下所示:
每个视图控制器都应该实现此方法以支持所需的方向。
另请检查
.plist
文件中的支持的界面方向
项。也许你的参数有误。For support all orientations your viewcontroller should implement
shouldAutorotateToInterfaceOrientation
like this:Every viewcontroller should implement this method for support required orientations.
Check also
Supported interface orientations
item in.plist
file. Maybe you have wrong parameters.在您的
secondviewcontroller
中尝试一下希望它有效..!!:)
Try this in your
secondviewcontroller
Hope it works..!!:)