返回 AppDelegate 重新创建 uitabbarcontroller

发布于 2024-11-25 16:08:58 字数 1553 浏览 1 评论 0原文

我有一个基于标签栏视图和欢迎屏幕的应用程序(导致登录或注册过程)。基本上,如果您已登录,您将直接进入标签栏视图,如果没有,您将进入欢迎屏幕,您可以在其中选择登录或注册。假设您登录或注册,我希望标签栏视图重新出现,但是,所有声明都在 AppDelegate 中。我怎样才能“返回”并调用tabbatcontroller?我的课程结构/流程是否正确?

重复一遍:

  1. 用户登录 ->第一个视图是选项卡栏视图
  2. 用户已注销 ->欢迎屏幕视图 -->登录/注册屏幕视图 -->选项卡栏视图

我正在寻找的是我需要在用户单击登录页面中的“登录”后调用的操作方法中编写什么:

-(IBAction)done:(id)sender {

?????

}

作为参考,我的 appDelegate 是:

  if(user signed in)
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:@"FirstTab" bundle:NSBundle.mainBundle];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:@"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, secondNavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

}
else
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    SigninTabBarTemplateViewController *landingPage = [[SigninTabBarTemplateViewController alloc] initWithNibName:@"SigninTabBarTemplateViewController" bundle:nil];
    self.window.rootViewController = (UIViewController *) landingPage;            
    [self.window makeKeyAndVisible];   
}

I have an app that is based on a tab bar view with a welcome screen (that leads to either signin or sign up process). basically, if you are logged in - you go straight to the tabbar view and if not, you go to the welcome screen, where you can chose to either go to sign in or sign up. assuming that you go to either sign in or sign up, i would like the tab bar view to reappear, however, all the declarations are in the AppDelegate. how can I "go back" and call the tabbatcontroller? is the structure / flow of my classes correct at all?

so to repeat:

  1. user signed in -> first view is tab bar view
  2. user logged out -> welcome screen view --> sign in / up screen view --> tab bar view

what i am looking for is what do i need to write in this action method that is called once the user clicks on "sign in" in the sign in page:

-(IBAction)done:(id)sender {

?????

}

for reference, my appDelegate is:

  if(user signed in)
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:@"FirstTab" bundle:NSBundle.mainBundle];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:@"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, secondNavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

}
else
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    SigninTabBarTemplateViewController *landingPage = [[SigninTabBarTemplateViewController alloc] initWithNibName:@"SigninTabBarTemplateViewController" bundle:nil];
    self.window.rootViewController = (UIViewController *) landingPage;            
    [self.window makeKeyAndVisible];   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独木成林 2024-12-02 16:08:58

您可以考虑很多选择。

使用委托可以轻松实现这一点。如果您想关闭以模态方式呈现的 VC,请为其指定一个 delegate 属性。需要时,代表将收到一条消息,让其解散 VC。使用委托的一个好方法是编写自定义协议。

例如:

// the delegate will conform to this protocol
@protocol SignInVCDelegate

// this method will be called when required
//
-(void)signInCompleted;

@end

现在,使您想要的对象符合该协议,例如应用程序委托。

// .h
#import "SignInVCDelegate.h"

@interface YourAppDelegate : NSObject <..., SignInDelegate> {
    ...
    SignInVC *signIn;
    ...
}

-(void)signInCompleted;

@end

实现如下所示:

// .m
...
-(void)signInCompleted {
    ...
    [signIn.view removeFromSuperview];
}

-(BOOL)applicationDidFinishLaunching {
    if(!logged) {
        ...
        [signIn setDelegate:self];
        [self.tabBarController presentModalViewController:signIn
                                                 animated:YES];
    }
}

现在给 signInVC 一个委托属性,该属性将在模态呈现之前设置,并在登录过程完成时向委托发送一条消息。

// in .h
@property(retain) id <SignInDelegate>delegate;

// in .m
@synthesize delegate;

-(IBAction)validateSignIn {
    ...
    [delegate signInCompleted];
}

您可以编写任何您想要的方法,这个示例很简单,并且为委托提供一些信息很有用。例如,在这种情况下,您可以传递用户名、用户 ID 或任何您想要的内容。

另一个简单的选择是使用通知。此选项可以让任何对象在发生某事时得到通知,只要它注册即可。给定与前面示例相同的对象,应用程序委托将注册通知,而登录视图控制器将发布通知。

// in app delegate .m
-(BOOL)applicationDidFinishLaunching {
    ...
    [[NSNotificationCenter defaultCenter]
     addObserver:self
        selector:@selector(signInCompleted)
            name:@"UserSignedInNotification"
          object:nil];
}

// in SignInVC.m
-(IBAction)validateSignIn {
    ...
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"UserSignedInNotification"
                   object:self];
}

有关委托和通知的更多信息,请参阅与对象通信

There are many options you can consider.

This can be easily achieved with the use of delegate. If you want to close the VC that you presented modally, give it a delegate property. The delegate will be sent a message when required, letting it dismiss the VC. A good way to go with delegate is to write a custom procotol.

For example :

// the delegate will conform to this protocol
@protocol SignInVCDelegate

// this method will be called when required
//
-(void)signInCompleted;

@end

Now, make the object you want conforms to that protocol, for example the app delegate.

// .h
#import "SignInVCDelegate.h"

@interface YourAppDelegate : NSObject <..., SignInDelegate> {
    ...
    SignInVC *signIn;
    ...
}

-(void)signInCompleted;

@end

The implementation looks like this :

// .m
...
-(void)signInCompleted {
    ...
    [signIn.view removeFromSuperview];
}

-(BOOL)applicationDidFinishLaunching {
    if(!logged) {
        ...
        [signIn setDelegate:self];
        [self.tabBarController presentModalViewController:signIn
                                                 animated:YES];
    }
}

Now give signInVC a delegate property, which will be set before being presented modally, and send the delegate a message when the sign in process is completed.

// in .h
@property(retain) id <SignInDelegate>delegate;

// in .m
@synthesize delegate;

-(IBAction)validateSignIn {
    ...
    [delegate signInCompleted];
}

You can write any method you want, this example is simplist, and it is useful to give the delegate some informations. In this case for example you could pass a user name, or user id, or what ever you want.

Another simple option is using notifications. This option lets any object informed when something happen, as long as it register for it. Given the same objects as the previous example, the app delegate will register for the notification, while the sign in view controller will post it.

// in app delegate .m
-(BOOL)applicationDidFinishLaunching {
    ...
    [[NSNotificationCenter defaultCenter]
     addObserver:self
        selector:@selector(signInCompleted)
            name:@"UserSignedInNotification"
          object:nil];
}

// in SignInVC.m
-(IBAction)validateSignIn {
    ...
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"UserSignedInNotification"
                   object:self];
}

More informations about delegates and notifications in Communicating with Objects.

寄与心 2024-12-02 16:08:58

您可以尝试在您知道用户已成功登录的方法中执行类似的操作。(假设 SignedInTabbarViewController 是您的 TabBarController)

   SignedInTabbarViewController *signedInTabbarViewController = [[SignedInTabbarViewController alloc] init];
   id mainDelegate = [[UIApplication sharedApplication] delegate];
   [self.navigationController.view removeFromSuperview];
   if( [mainDelegate respondsToSelector:@selector(setViewController:)]) {
       [mainDelegate setViewController:signedInTabbarViewController];
   }
   UIWindow   *mainWindow = [mainDelegate window];
   [mainWindow addSubview: signedInTabbarViewController.view];
  [signedInTabbarViewController release];

You could try doing something like this in the method where you know the user has successfully logged in. (Assuming SignedInTabbarViewController is your TabBarController)

   SignedInTabbarViewController *signedInTabbarViewController = [[SignedInTabbarViewController alloc] init];
   id mainDelegate = [[UIApplication sharedApplication] delegate];
   [self.navigationController.view removeFromSuperview];
   if( [mainDelegate respondsToSelector:@selector(setViewController:)]) {
       [mainDelegate setViewController:signedInTabbarViewController];
   }
   UIWindow   *mainWindow = [mainDelegate window];
   [mainWindow addSubview: signedInTabbarViewController.view];
  [signedInTabbarViewController release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文