开发者

Changing views and delegate event listeners

Hey everyone :) I'm having a small issue and i'd love to hear from your experience how would you deal with this.

My main screen is a login screen, and after a successful login I'm initializing a new view and switching to it in the following way i found here on SO:

    // Switch views 
    [txtUser resignFirstResponder];
    [txtPass resignFirstResponder];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[self view] cache:YES];
    [UIView setAnimationDuration:0.6];

    UIViewController *homeView = [[[UIViewController alloc] initWithNibName:@"HomeView" bundle:nil] autorelease];
    [self.view insertSubview:homeView.view atIndex:0];

    [UIView commitAnimations];

After login i wan't to switch a view when the device rotates, but "willRotateToInterfaceOrientation" doesn't catch anything inside my sub view, it only catches it if i put it in the parent.

So i was thinking about three options

  • Find a way to catch this rotation event inside the sub view
  • After login, switch views completely instead of just adding it as a sub view (so it would be a parent by itself?) - i dont know if its possible and if is im not sure how to do it , but i'd love to hear your experience.
  • Find some way to go into this rotated view from the parent (which is not a really good option i think)

I'm sorry if this is a little scrambled, hope you can understand and if not i'd love to elaborate and add more needed info , if any is needed :)

Thanks in advance, Shai.

Edit: I've added the navigation controller like @Ahmed suggested and pushed the new view but i'm getting an empty gray view instead of my HomeView.xib, any ideas on this perhaps?

On AppDelegate.h

开发者_StackOverflow社区

<-- language: lang-cpp -->

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

On AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window.rootViewController = self.viewController;

// Initialize navigation controller
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
self.navigationController = aNavigationController;
[aNavigationController release];
[_window addSubview:[_viewController view]];

[self.window makeKeyAndVisible];
return YES;
}

And then , after a successful login i tried

// Switch views 

    [txtUser resignFirstResponder];
    [txtPass resignFirstResponder];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[self view] cache:YES];
    [UIView setAnimationDuration:0.6];

    UIViewController *homeView = [[[UIViewController alloc] initWithNibName:@"HomeView" bundle:nil] autorelease];
    [self.navigationController pushViewController:homeView animated:YES];
    [self dismissModalViewControllerAnimated:YES];

    [UIView commitAnimations];

But i'm getting an empty gray view (like a new one) instead of my HomeView... any idea how could that happen? ...

Thanks again you guys are lifesavers!


You should navigate to your another view instead of adding it as subView.

[self.navigationController pushViewController:homeView animated:YES];  

but for that you need to have instance of navigationController in your app. Or you should start with a navigation based template if you ain't using one. You're just adding your homeViewController's View on your login view so only the view getting added and homeViewController will not get any event such as orientation change.

Edit:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

YourRootViewController *rootViewController = [[YourRootViewController alloc] init];

UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.viewController = aNavigationController;
[aNavigationController release];
[_window addSubview:[_viewController view]];

   // self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}


Wow, I just literally had the exact same problem. I solved it by using two different UIWindow objects. The rootViewController of each UIWindow will receive the rotation notifications. So, in your app delegate, create a separate UIWindow that can store the login screen. Just set the other window's hidden property to YES until the login has succeeded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜