2011-09-15 3 views
1

オリエンテーションを使用してアプリをiPadに書き込んでいます。 のApp-Delegate.hは窓を持っている、のUIViewController、UINavigationControllerとUITabbarController:多くのビューとコントローラ(iPad)のオリエンテーションの問題

@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" -methodを使用しています。

は私が使用してビューを変更します。

[UIView beginAnimations:nil context:NULL]; 

、その後

[loginRVC.view removeFromSuperview]; 
[_window addSubview:chooseCameraRVC.view]; 

および他の方法の周りにも、OFC。

私の問題は、2番目のビュー(chooseCameraRVC)にあり、方向を切り替えて、最初のビューに戻って、その回転しないことです。アニメーションが完了した後、自動回転します。

すべてのビューの "shouldAutorotateToInterfaceOrientation"メソッドの呼び出しのように多くのことを試しましたが、ウィンドウからビューを削除するのではなく...今は成功しません。

これはシミュレータの「機能」ですか? (私は望んでいない)。

Pls help me。 シャーキー


Ok。ここに紹介するソースコードを用意しました。

注:[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

答えて

2

今私は私のミスを発見しました。私は、アプリケーションの代理人の変数としてビューを持っていることがわかります。 2番目のビューが方向を変えた場合、他のビューは方向性を変更しませんでした。ビューが現在変更されている場合、「新しい」アニメーションの後の向きの変更が認識されるため、アニメーションの実行中は「新しい」ビューの向きが正しくありません。

ビューを切り替えたい場合は、正しい向きで初期化されるため、新しいビューを作成してください。

種類について $ h @ rky

0

すべての方向はあなたのViewControllerは、このようなshouldAutorotateToInterfaceOrientationを実装する必要があります。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

すべてのViewControllerは、サポートに必要な姿勢のために、このメソッドを実装する必要があります。

Supported interface orientations.plistの項目もチェックしてください。たぶん間違ったパラメータがあります。

+0

すべてが完了しました。ビュー自体を見れば本当にうまくいきます。しかし、あなたが向きを変えて別のビューに切り替えると、アニメーションの後に自動回転してしまい、これはかなり悪く見えます。 – Sharky

+0

Hm、ブロックを使ってアニメーションを作ろう: '[UIView animateWithDuration:0.4fアニメーション:^ {_myView.frame = fullRect; }]; ' – beryllium

+0

nope - アニメーションの時間を除いて何も変わりません。 – Sharky

0

あなたsecondviewcontroller

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
UIViewController *controller = [self.navigationController.viewControllers objectAtIndex:0]; 
[controller shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 

return YES; 
} 

でこれを試してみてください、それが動作しますホープ..!:)

+0

いいえ。最初のビューは、UIViewController内のUIViewだけなので、2番目のビューには接続されません。たぶん問題だよ!? – Sharky

関連する問題