从委托方法执行segue

时间:2011-11-10 14:58:55

标签: objective-c delegates self

我有一个分段控件,当点击时,用户将被迫输入密码。

成功检查后,模态屏幕应该消失,并且在控制器中使用分段控件调用方法,并且应该对另一个控制器执行segue。

segue本身可以正常工作,但不能在登录检查后使用。我认为这是“自我”的问题......

代表类代码:

@protocol LoginUserControllerDelegate
    - (void)proceedAfterLogin:(BOOL)proceedToNextController;
@end

@interface LoginUserController : UIViewController {
    IBOutlet UITextField *loginPWD;
    id<LoginUserControllerDelegate> _delegate;

}

@property (nonatomic, retain) UITextField *loginPWD;
@property (nonatomic, assign) id<LoginUserControllerDelegate> delegate;

- (void)checkLogindata; // checks for correct password
@end

@implementation LoginUserController

@synthesize loginPWD;
@synthesize delegate = _delegate;

- (void)checkLogindata {
    ...
    if ([tempDBPWD isEqualToString:tempLoginPWD]) {
         if (_delegate != nil) {
               [self dismissModalViewControllerAnimated:YES];
               [_delegate proceedAfterLogin:YES];
           }

    }
    ...
}

@end

分段控制器的方法:

@interface StartViewController : UIViewController <LoginUserControllerDelegate> {
    UISegmentedControl *segmentedControl;
    LoginUserController *_loginUserController;
}

@property (nonatomic,retain) IBOutlet UISegmentedControl *segmentedControl;
@property (nonatomic, retain) LoginUserController *loginUserController;

- (IBAction)segmentedControlIndexChanged:(id)sender;
@end


@implementation StartViewController

@synthesize segmentedControl;
@synthesize loginUserController = _loginUserController;


- (IBAction)segmentedControlIndexChanged:(id)sender
{
    if (self.segmentedControl.selectedSegmentIndex == 0) {
       // get delegate
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIStoryboard *storyboard = appDelegate.window.rootViewController.storyboard;
        LoginUserController *loginUserControllerView = [storyboard    instantiateViewControllerWithIdentifier:@"LoginUserViewID"];    
        loginUserControllerView.delegate = self;
        // now show screen
        [self presentModalViewController:loginUserControllerView animated:YES];

    } else {

    }

}

- (void)proceedAfterLogin:(BOOL)proceedToNextController
{
    if (proceedToNextController) {
        // yes, correct login now perform the segue to the controller
        [[self segmentedControl] setSelectedSegmentIndex:UISegmentedControlNoSegment];
        [self performSegueWithIdentifier:@"NextControllerSegueID" sender:self];

    }

}

@end

我认为[self performSegueWithIdentifier:@"NextControllerSegueID" sender:self];是问题,因为自我不正确。这是错误出现的地方,我如何实现我想要的行为?

1 个答案:

答案 0 :(得分:1)

您已关闭,但您需要返回委托,然后调用dismissModalViewControllerAnimated:YES。模态控制器不能解除自身,因此您需要进行登录,回调委托,并让委托解雇模态控制器。