2013-05-10 37 views
0

我试图遵循这一问题设置为登录序列的代表: - How do I set up a simple delegate to communicate between two view controllers?建立一个代表,以确定登录序列已完成

在我的应用程序有一个主视图(MESHomeViewController)。 我也有一个登录结构如下:

  1. MESLoginNavController
  2. 随着MESWelcomeViewController
  3. 的根视图控制器有可被推送到堆栈也MESLoginViewController和MESSignupViewControllers。

我的目标是有一个委托方法,该方法只调用的时候在我使用的是委托作为,所以我要看看我能不能用viewDidLoad中的观点经常重载/在不同的场合表示,用户登录当用户完成登录时。

用户可以通过Welcome控制器(通过Facebook)或登录控制器(通过常规方法)并通过注册(通过注册)登录。

这是我曾尝试下面来实现: MESHomeViewController.h

#import "MESWelcomeViewController.h" 

@interface MESHomeViewController : UIViewController <LoginViewControllerDelegate> 

@end 

MESHomeViewController.m我检查,如果用户登录,如果他们没有完成以下:

NSLog(@"not logged in"); 
MESWelcomeViewController *loginNavVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WelcomeVC"]; 
    loginNavVC.delegate = self; 
[self presentModalViewController:loginNavVC animated:NO]; 

MESWelcomeViewController.h我有以下几点:

@protocol LoginViewControllerDelegate; 

@interface MESWelcomeViewController : UIViewController <NSURLConnectionDataDelegate> 

@property (nonatomic, weak) id<LoginViewControllerDelegate> delegate; 

@end 



@protocol LoginViewControllerDelegate <NSObject> 

- (void)didLoginUser; 

@end 

一旦用户完全通过自定义方法登录, MESWelcomeViewController.m一旦用户在

NSLog(@"%@",self.delegate); 
    if ([self.delegate respondsToSelector:@selector(didLoginUser)]) { 
     [self.delegate didLoginUser]; 
    } 

然而登录,委托方法不被调用时,它出现在self.delegate是空的welcomeViewController。我想可能我应该根据上述设置将代理设置为Login Nav Controller,但是我不确定如何从当前在nav上推送的视图控制器调用委托方法?

回答

0

所以,只需确认一下,您就拥有MESHomeViewController,这是您的主要观点。从这里,您将MESWelcomeViewController推入堆栈。 MESWelcomeViewController可以执行一些登录的东西,一旦完成,调用[self。代表didLoginUser]。

如果是这种情况,当您初始化MESWelcomeViewController时,您需要设置委托。您可以通过多种方式轻松完成此操作。你可以为MESWelcomeViewController一个初始化函数,该函数在一个id

-(id) init:(id<LoginViewControllerDelegate>)myDelegate { 
    self = [super init]; 
    if (self){ 
     self.delegate = myDelegate; 
     etc... 
    } 
} 

此外,在创建的MESWelcomeViewController之后,但推动它之前,设置委托财产

MESWelcomeViewController *welcomeView = [[MESWelcomeViewController alloc] init]; 
[welcomeView setDelegate:self]; 
// push view stuff 

编辑:

我给出的init函数不会自动调用,它必须被显式调用,而不是默认的初始化程序。如果您正在使用故事板,则可能应该使用设置委托属性的替代方法。我从来没有故事板采用过,但你应该能够做到这样的事情:

MESWelcomeViewController *welcomeView = [self.storyboard instantiateViewControllerWithIdentifier:@"WelcomeVC"]; 
[welcomeView setDelegate:self]; 
[self presentModalViewController:welcomeView animated:NO]; 

编辑2: 最好的选择是使用通知来代替。任何想要监听的人都会向NSNotificationCenter添加一个观察者,并在事件发生时发布通知。

例子:

在MESHomeViewController的初始化或viewDidLoad中,将以下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginUser) name:@"LoginCompleteNotification" object:nil]; 

然后,每当你完成一个登录,请执行下列操作:

[[NSNotificationCenter defaultCenter] postNotificationName:@"LoginCompleteNotification" object:nil]; 

最后,确保你停止听Dealloc中任何正在收听的内容

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"LoginCompleteNotification object:nil] 
+0

谢谢,我试图使用你的init,但是当我NSLog的self.delegate(在登录完整的块内)我仍然越来越(空)?登录按下面的''[self presentModalViewController:[self.storyboard instantiateViewControllerWithIdentifier:@“WelcomeVC”] animated:NO]; ''这会将故事板中的标识符推向LoginNavController(以Welcome Controller作为根)。我不确定如何实现你的第二点,同时以这种方式呈现模态 – StuartM 2013-05-10 15:35:59

+0

事实上,如果我将该init函数添加到MESWelcomeViewController中,它永远不会被初始化调用?这是对的吗? – StuartM 2013-05-10 15:37:51

+0

我给出的init函数不会自动调用,它必须显式调用,而不是默认的初始化程序。如果您正在使用故事板,则可能应该使用设置委托属性的替代方法。我从来没有使用故事板,但你应该可以做这样的事情: MESWelcomeViewController * welcomeView = [self.storyboard instantiateViewControllerWithIdentifier:@“WelcomeVC”]; [welcomeView setDelegate:self]; [self presentModalViewController:welcomeView animated:NO]; – Doc 2013-05-10 15:43:10

0

您的ViewController可能被其容器释放,或者您设置的delegate属性太快。

我会建议增加这第一个视图控制器:

- (void) dealloc 
{ 
    NSLog(@"deallocing"); 
} 

只是为了排除第一个选项。

要检查第二个选项,请在设置其delegate后打印loginNavVC,确保它不是nil

+0

我已经添加了以下内容来释放它,看它是否被释放,它不是。 NSLog(@“%s”,__FUNCTION__); (对于主视图控制器)。我还添加了一个日志'NSLog(@“%@”,loginNavVC.delegate);'它会像应该那样返回MESHomeViewController。但是,一旦登录,self.delegate Log仍然为空? – StuartM 2013-05-10 13:37:54

+0

我认为这是因为这行“”MESWelcomeViewController * loginNavVC = [self.storyboard instantiateViewControllerWithIdentifier:@“WelcomeVC”];“”实际上指向LoginNavController,它是标识符WelcomeVC,因为它具有作为根视图控制器的欢迎。所以它就像我设置loginNavController委托而不是实际的欢迎视图控制器,但我不确定如何做到这一点? – StuartM 2013-05-10 13:42:52

相关问题