代表是零,使用storyboard embed segue

时间:2014-02-16 22:34:49

标签: ios iphone objective-c delegates segue

我正在使用像facebook这样的侧边菜单创建应用。 我有两个containerView,它们与embed segue连接到它们的viewControllers。

当我在我的sideVC中滑动时,我需要在我的mainVC中调用方法来隐藏侧边菜单,我尝试使用委托但是它没有:(

让我的父类SlidingVC

SlidingVC.h

@class SlidingVC;

@protocol SlidingVCDelegate

-(void)slidingVCDidFinishLeft:(SlidingVC *)svc;
-(void)slidingVCDidFinishRight:(SlidingVC *)svc;

@end

@interface SlidingVC : UIViewController

@property(nonatomic, assign) id<SlidingVCDelegate> delegate;

@end

我的左右控制器是他的孩子

@interface RightPanelSlidingVC : SlidingVC
------------------------------------------
@interface LeftPanelSlidingVC : SlidingVC

我的mainVC包含我的侧边菜单并使用SlidingVCDelegate

@interface MainViewController : UIViewController <SlidingVCDelegate>

所以我需要调用mainVC方法-(void)slidingVCDidFinishLeft:(SlidingVC *)svcright

在我的leftSideVC中,当我轻扫时,我打电话:

- (IBAction)swipeLeft:(UISwipeGestureRecognizer *)sender {
    [self.delegate slidingVCDidFinishLeft:self];
    NSLog(@"Swipe left");
}

在我的mainVC

@interface MainViewController ()

@property (strong, nonatomic) LeftPanelSlidingVC *lvc;
@property (strong, nonatomic) RightPanelSlidingVC *rvc;

@end
......
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.lvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Left VC"];
    self.rvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Right VC"];
}
-(void)slidingVCDidFinishLeft:(SlidingVC *)svc{
    NSLog(@"Swipe left sucess!");
    [self animateSlidingVC:svc reveal:NO];
    [self shadowOpasity:0.0f from:2.0f to:0.0f inView:LeftContainer];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"embedRightVC"]) {
        self.rvc = (RightPanelSlidingVC *)segue.destinationViewController;
        self.rvc.delegate = self;
    }
    if ([[segue identifier] isEqualToString:@"embedLeftVC"]) {
        self.lvc = (LeftPanelSlidingVC *)segue.destinationViewController;
        self.lvc.delegate = self;
    }
}

但在mainVC我的代表是零,我不知道为什么。 因为我认为在嵌入segue的地方存在问题

app structure

storyboard

抱歉我的英文

2 个答案:

答案 0 :(得分:0)

据我所知,默认情况下viewDidLoad未在故事板中自动调用。您需要使用awakeFromNib而不是viewDidLoad。这是self.lvcprepareForSegue:sender为空的原因。

viewDidLoad更改为awakeFromNib

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.lvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Left VC"];
    self.rvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Right VC"];
}

答案 1 :(得分:0)

问题发生在prepareForSegue 我的面板嵌入在navController中,所以我需要说我的uinavigationController是目标控制器

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"embedRightVC"]) {
        if ([segue.destinationViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navController = segue.destinationViewController;
            if ([navController.childViewControllers lastObject]) {
                RightPanelSlidingVC *rightVC = navController.childViewControllers[0];
                [self setRvc:rightVC];
                [self.rvc setDelegate:self];
                NSLog(@"Right Delegate");
            }
        }
    }
    if ([[segue identifier] isEqualToString:@"embedLeftVC"]) {
        if ([segue.destinationViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navController = segue.destinationViewController;
            if ([navController.childViewControllers lastObject]) {
                LeftPanelSlidingVC *leftVC = navController.childViewControllers[0];
                [self setLvc:leftVC];
                [self.lvc setDelegate:self];
                NSLog(@"Left Delegate");
            }
        }
    }
}