隐藏LoginViewController或关闭到parentView

时间:2015-12-08 08:45:56

标签: ios objective-c unwind-segue

这是正常的LoginView调用我的应用程序中的特定操作

sourceViewVontroller

if ([password length] == 0) {

     loginViewController *seatView = [mainStory instantiateViewControllerWithIdentifier:@"loggingView"];



    [self presentViewController:login animated:YES completion:nil];

}

它检查用户在调用此操作时是否尚未登录,因此将其重定向到loginViewController,然后

loginViewController

UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

DestinationViewController *Dest = [mainStory instantiateViewControllerWithIdentifier:@"destView"];


[self presentViewController:Dest animated:YES completion:nil];

它转到需要登录的destinationView,现在我的问题是如何返回(关闭not presentmodalView)到sourceViewController,或者简单地说,如何在成功时从队列中删除loginViewController并直接关闭源?

destinationViewController

// Tried to use presentView .. but i dont need to reload sourceView, just dismiss to it !

//[self presentViewController:srcView animated:YES completion:NULL];

//This is what am doing ..

[self dismissViewControllerAnimated:NO completion:nil];

或者只是让我知道是否有其他专业方法来执行此登录流程

3 个答案:

答案 0 :(得分:1)

我建议你另辟蹊径:

首先在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

您将检查用户是否登录。如果登录,您会将rootView设置为destinationViewController。如果未将rootView设置为loginViewController

SampleCode:

UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if (logged) {

   DestinationViewController *Dest = [mainStory instantiateViewControllerWithIdentifier:@"destView"];
self.window.rootViewController = Dest;
} else {
   LoginViewController *seatView = [mainStory instantiateViewControllerWithIdentifier:@"loggingView"];
   self.window.rootViewController = seatView;
}

退出时,您只需致电notificationdelegaterootViewController的{​​{1}}设为window

答案 1 :(得分:0)

使用展开细分

SourceViewController

中添加方法

对于Swift

@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {

}

对于Objective-C

- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue {

}

您可以通过两种方式返回SourceViewController

  1. 使用Storyboard,例如,当UIButton操作触发时。
  2. 在代码中使用performSegueWithIdentifier
  3. <强>故事板:

    通过UIButton

    中的DestinationViewControllerExit segue中的SourceViewController控制拖动

    enter image description here

    代码:

    1. 通过UIButton
    2. 中的DestinationViewControllerExit segue中的SourceViewController控制拖动

      enter image description here

      1. 需要回到performSegueWithIdentifier:@"ExitToSourceViewController"
      2. 时致电SourceViewController

答案 2 :(得分:0)

请勿使用presentViewController来启动登录控制器。 完整流程: 在Singleton类中。设置全局属性。

@property (assign)BOOL isUserLoggedIn;

当用户登录或注销时,请将此变量设置为true或false。

在LoginViewController中设置枚举。

typedef enum {

    DestViewControllerOne =1,

    DestViewControllerTwo

} SignInType;

在此枚举中,将所有视图控制器放在要进行登录检查的位置。 在登录视图控制器中设置属性以保存源控制器值 -

@property (nonatomic, assign) NSInteger signInType;

在登录控制器中设置委托以在成功登录后重定向 -

@protocol SignInProtocolDelegate <NSObject>
@optional

-(void) signInSuccess:(NSInteger) signInType;

@end

在登录控制器中使用该委托创建属性 -

@property (nonatomic, assign) NSObject<SignInProtocolDelegate>* delegate;

现在,在继续执行所需的视图控制器之前,请执行以下检查 -

if(![[TESingleton shareData] isUserLoggedIn]){

                [self funcNavigateToSignInWithAlert:YES withSignInType:proceedToDestViewControllerOne];

            }
            else
            {
                [self proceedToDestViewControllerOne];
            }

-(void)funcNavigateToSignInWithAlert:(BOOL)showAlert withSignInType:(NSInteger) signIntype
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:STORYBOARDNAME bundle:nil];
    LoginViewController *viewController = (LoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    [viewController setSignInType:signIntype];
    [viewController setDelegate:self];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [[SlideNavigationController sharedInstance] presentViewController:navController animated:YES completion:nil];
}

这样,它将带来登录页面。

下一步 - 成功登录后,在登录控制器中执行此操作:

[self dismissViewControllerAnimated:YES completion:nil];

[[self delegate] signInSuccess:self.signInType];

在我们上面写的源控制器中导入登录委托方法 -

-(void) signInSuccess:(NSInteger) signInType
{
    switch (signInType)
    {
        case DestViewControllerOne:
            [self performSelector:@selector(proceedToDestViewControllerOne) withObject:nil afterDelay:0.5];
            break;

        case DestViewControllerTwo:
            [self performSelector:@selector(proceedToDestViewControllerTwo) withObject:nil afterDelay:0.5];
            break;


        default:
            break;
    }
}

在源视图控制器中实现这些方法 -

-(void) proceedToDestViewControllerOne
{
    //Restricting navigation to signin
    //Addded by vikas Jul 8,2015
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:STORYBOARDNAME bundle:nil];

    DestViewcontrollerOne *objDestViewcontrollerOne = (DestViewcontrollerOne *)[storyboard instantiateViewControllerWithIdentifier:@"DestViewcontrollerOne"];

    [self.navigationController objDestViewcontrollerOne animated:YES];

}

这是一个完整的过程。