无法使用委托将数据从MainViewController发送到ContainerView

时间:2015-03-06 07:50:49

标签: ios objective-c segue uicontainerview

我创建了简单的ipad应用程序,我想将值从MainViewController发送到ContainerViewController。我对这个应用程序的整个目标是从Container(A)向MainViewController发送值,然后将MainViewController发送到Container(B)。我能够从Container(A)向MainViewController发送值,但我无法将数据从MainViewController发送到Container(B)。我认为我对prepareForSegue的使用是错误的。 ?我添加了图片来描述我的情况。

提前致谢。

A_ContainerViewController.h

@class A_ContainerVC;
@protocol A_ContainerVCDelegate <NSObject>
-(void)sendValue:(NSString *)Name;
@end

@interface A_ContainerViewController : UIViewController

@property(nonatomic,assign) id <A_ContainerVCDelegate> delegate

- (IBAction)btnSend:(id)sender;

@end

A_ContainerViewController.m

- (IBAction)btnSend:(id)sender {

    NSString *text=@"Tutorial";

    [self.delegate sendValue:text];

}

ViewController.h

#import <UIKit/UIKit.h>
#import "A_ContainerViewController.h"

@class MainVC;
@protocol MainVCDelegate <NSObject>  

-(void)sendValue2:(NSString *)Name;

@end

@interface ViewController : UIViewController

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

@property (strong, nonatomic) IBOutlet UILabel *lblVC;

@end

ViewController.m

@interface ViewController () <A_ContainerVCDelegate>

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"A"]) {


        [(A_ContainerViewController *)segue.destinationViewController setDelegate:self];

    } 
}


-(void)sendValue:(NSString *)Name{

    lblVC.text = Name;
    [self.delegate sendValue2:Name];

}

B_ContainerViewController.h

    #import <UIKit/UIKit.h>
    #import "ViewController.h"
    #import "A_ContainerViewController.h"
    @interface B_ContainerViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *lbl_B_VC;
@end

B_ContainerViewController.m

    @interface B_ContainerViewController ()<MainVCDelegate>
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"B"]) {


            [(ViewController *)segue.destinationViewController setDelegate:self];

            /*
             LayersViewController *layersViewController = (LayersViewController*)segue.destinationViewController;
             layersViewController.name=@"Deneme";
             */


        }

    }

-(void)sendValue2:(NSString *)Name{

    lbl_B_VC.text=Name;

}

enter image description here

3 个答案:

答案 0 :(得分:1)

从故事板中,控制将A和B控制器的IBOutlet拖动到主控制器中。然后,在MainViewController的viewDidLoad方法中,将容器A和B的两个委托都设置为self。

答案 1 :(得分:1)

在B_ContainerViewController.m中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"B"]) {


        [(ViewController *)segue.destinationViewController setDelegate:self];

        /*
         LayersViewController *layersViewController = (LayersViewController*)segue.destinationViewController;
         layersViewController.name=@"Deneme";
         */


    }

}

为什么这个代码在B_ContainerViewController.m中?因为如果我正确理解了你的场景,你可以在ViewController类中添加A和B容器VC。因此,您应该在ViewController.m

的prepareForSegue中添加B作为委托
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"A"]) 
    {
        [(A_ContainerViewController *)segue.destinationViewController setDelegate:self];    
    }
    else if([segue.identifier isEqualToString:@"B"])
    {
        self.delegate = (B_ContainerViewController *)segue.destinationViewController;//B should conform to MainVCDelegate
    } 
}

答案 2 :(得分:0)

这里有一些我不会看到的东西...... 这是委托模式的错误使用!如果要将值传递给另一个UIViewController,则必须在另一个值中保存对它的引用。然后,您可以在.h文件中指定一个公共方法,并从其他控制器中调用它。

例如,在 B_ContainerViewController

// .h file
@interface B_ContainerViewController : UIViewController

@property (strong, nonatomic) NSString *labelTextValue;
@end
// .m file
@interface B_ContainerViewController ()
{
    IBOutlet UILabel *lbl_B_VC;
}
@end
@implementation
@synthesize labelTextValue = _labelTextValue;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"B"]) 
    {

        /*
         LayersViewController *layersViewController = (LayersViewController*)segue.destinationViewController;
         layersViewController.name=@"Deneme";
         */
    }

}

-(void)setLabelTextValue:(NSString *)labelTextValue
{
     _labelTextValue = labelTextValue;
    [lbl_B_VC setText: _labelTextValue];
}

因此,在控制器 A 中,在IBAction上,你必须这样做:

referenceToControllerB.labelTextValue = @"TEXT";