Passing data between Tabs using delegate not worki

2019-06-05 18:12发布

问题:

I want to pass data from second tab to first tab using delegates.But delegate method not triggered.here is the code (I m using storyboard)

in SecondViewController.h

#import <UIKit/UIKit.h>

 @class SecondViewController;
 @protocol SecondViewControllerDelegate <NSObject>

 -(void) sliderValueDidChanged: (SecondViewController *)controller data:(float)      sliderValue;

 @end

 @interface SecondViewController : UIViewController{
__weak id<SecondViewControllerDelegate> delegate;   
 }
 - (IBAction)sliderPressed:(id)sender;

 @property (weak, nonatomic) IBOutlet UISlider *mySlider;
 @property(weak,nonatomic) id<SecondViewControllerDelegate> delegate;

 @end

in SecondViewController.m

#import "SecondViewController.h"


@interface SecondViewController ()

@end

 @implementation SecondViewController
 @synthesize mySlider;
 @synthesize delegate;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
 {
[super viewDidLoad];
// Do any additional setup after loading the view.
 }

 - (void)viewDidUnload
 {
[self setMySlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

- (IBAction)sliderPressed:(id)sender {
float value = mySlider.value;

[self.delegate sliderValueDidChanged:self data:value];
NSLog(@"%@",self.delegate);
}
@end

FirstViewController.h

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

@interface FirstViewController : UIViewController<SecondViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@end

in FirstViewController.h

#import "FirstViewController.h"


@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize myLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   SecondViewController *svc = [[SecondViewController alloc]init];
   svc.delegate = self;




}

- (void)viewDidUnload
{
    [self setMyLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) sliderValueDidChanged: (SecondViewController *)controller data:(float) sliderValue{
    myLabel.text = [[NSString alloc]initWithFormat:@"%f",sliderValue];
    NSLog(@"delegate called");
}

@end

I am trying to set a slider in second tab and send the sider value to first tab which has a label. self.delegate printing null and delegate method never called.

回答1:

Simply enough, as you already have the delegation code, only change the line where you create a SecondViewController object. Rather than creating a new one, you should have a reference to the one that the tab bar will show.

In the viewDidLoad of FirstViewController, Change

SecondViewController *svc = [[SecondViewController alloc]init];

to

//assuming you have 2 view controllers in the tab bar controller, first being FirstViewController
SecondViewController *svc = [self.tabBarController.viewControllers objectAtIndex:1]; 

and that should do it



回答2:

How do you actually get to the second tab? You're creating an instance of it in viewDidLoad, but if you switch tabs, that instance won't be used to show on screen. The system will create another instance which it'll use. You can access this by accessing the tab bar controller's viewcontrollers property and checking for SecondViewController.

EDIT: You could also question your design. I don't know much about your app, but chances are your first tab shouldn't really be the delegate for your second tab. If you want to pass along data, consider using NSNotificationCenter or persistent storage.